最新消息:20190717 VPS服务器:Vultr新加坡,WordPress主题:大前端D8,统一介绍入口:关于

【已解决】Antd Pro中翻页出错:发出的请求针对的是不存在的记录

Web crifan 182浏览 0评论

现有一个前端Antd Pro+后端Django的内容管理系统,现在是:

点击第2页或其他页,结果会报错,且一直loading:

需要搞清楚问题原因并解决。

错误:

发出的请求针对的是不存在的记录,服务器没有进行操作。
请求 http://x.x.x.:112233/api/v1/scripts/group_owner_script_list/?page=2&page_size=20 出错 404

突然想起来:

或许和之前

删除和重建NaturlingCmsServer文件夹有关系?

不管了,去看看代码

看代码是:

    @list_route(
        methods=['get'],
        url_path='group_owner_script_list',
        url_name='group_owner_script_list',
        pagination_class=StandardResultsSetPagination,
        permission_classes=[
            IsAuthenticated, IsUserScriptFunctionGroup,
        ])
    def group_owner_script_list(self, request):
        filter_condition = self.generateQueryFilterCondiction(request)

和:

apps/script/permissions.py

class IsUserScriptFunctionGroup(permissions.BasePermission):
    def has_permission(self, request, view):
        """
            topic/dialog的权限,组成员都能查看
        """
        user = request.user
        if 'script_function_group.script_function_group' in user.get_group_permissions():
            return True
        else:
            return False

再去搜:

django get_group_permissions

django.contrib.auth | Django documentation | Django

“get_group_permissions(obj=None)¶

Returns a set of permission strings that the user has, through their groups.

If obj is passed in, only returns the group permissions for this specific object.”

django.contrib.auth.backends | Django documentation | Django

Customizing authentication in Django | Django documentation | Django

Django权限机制的实现 – 简书

“user.get_all_permissions()方法列出用户的所有权限,返回值是permission name的list

user.get_group_permissions()方法列出用户所属group的权限,返回值是permission name的list”

基本上确定是:

此处返回的get_group_permissions的是有问题的。

所以要去找为何有问题。为何之前是正常的。

所以去备份在线mysql到本地,去调试看看什么情况。

另外看到初始化:

添加上面异步创建的超级用户至默认功能组
`python3 ./manage.py add_initial_function_group`

结果找不到:

add_initial_function_group

然后发现是command的文件名:

【已解决】Mac本地给mysql数据库改名

然后本地恢复:

mysql -uroot -pcrifan_mysql naturling < aliyun_rds_mysql_naturling_180725.sql

然后再去调试代码找原因

结果本地localhost竟然是翻页没问题:

那就奇怪了。

本地重新启动mysql

➜  ~ /usr/local/mysql/support-files/mysql.server stop
Shutting down MySQL
.... SUCCESS!
➜  ~ /usr/local/mysql/support-files/mysql.server start
Starting MySQL
. SUCCESS!

重新启动cms的server:

➜  NaturlingCmsServer git:(master) ✗ python3 ./manage.py runserver_plus 65000
* Running on http://127.0.0.1:65000/ (Press CTRL+C to quit)
* Restarting with stat
Performing system checks...

System check identified no issues (0 silenced).

重启web端:

➜  NaturlingCmsWeb git:(master) ✗ npm start

然后再去看看效果

试了半天,终于模拟线上的问题了:

然后就可以去调试问题了。

然后发现:

代码:

class IsUserScriptFunctionGroup(permissions.BasePermission):
    def has_permission(self, request, view):
        """
            topic/dialog的权限,组成员都能查看
        """
        logger.info("IsUserScriptFunctionGroup: request=%s,view=%s", request, view)
        user = request.user
        logger.info("user=%s", user)
        userGroupPermissions = user.get_group_permissions()
        logger.info("userGroupPermissions=%s", userGroupPermissions)

        if 'script_function_group.script_function_group' in user.get_group_permissions():
            return True
        else:
            return False

log:

INFO|20180725 10:32:01|permissions:has_permission:16|userGroupPermissions={'script.change_history', 'user.change_functiongroup', 'admin.add_logentry', 'admin.delete_logentry', 'script.change_keywordrel', 'contenttypes.add_contenttype', 'user.change_user', 'script.change_script', 'sites.delete_site', 'contenttypes.change_contenttype', 'admin.admin', 'sites.add_site', 'user.delete_user', 'admin.change_logentry', 'admin_function_group.admin_function_group', 'auth.change_group', 'auth.add_group', 'sessions.add_session', 'script.add_history', 'script.delete_script', 'user.add_user', 'script.delete_topic', 'user.add_functiongroup', 'auth.change_permission', 'contenttypes.delete_contenttype', 'script.add_script', 'script.delete_dialog', 'script.add_review', 'auth.delete_group', 'auth.add_permission', 'script.add_dialog', 'script.change_dialog', 'script.delete_keywordrel', 'sessions.delete_session', 'sessions.change_session', 'auth.delete_permission', 'script.change_topic', 'script.add_keywordrel', 'script.delete_review', 'script_function_group.script_function_group', 'script.change_review', 'user.delete_functiongroup', 'script.add_topic', 'sites.change_site', 'script.delete_history'}

 

是有script_function_group.script_function_group的

不是权限问题。

那么就是:

127.0.0.1 - - [25/Jul/2018 10:32:01] "GET /api/v1/scripts/group_owner_script_list/?page=2&page_size=20 HTTP/1.1" 404 -

的page方面的问题了。

然后通过调试基本上明白错误的逻辑了:

此处出问题的步骤是:

先去组员列表统计

中,点击某个用户,进去组员列表页面,且url中是带过滤参数:某个用户的id的:

http://localhost:8000/#/script/group-owner-script-list?user_id=44c3de7f-a0e4-4a62-8f64-720044ad6d03

然后点击其他页时:

调用的接口中,并没有把之前的user的id传过去,而是传递了当前的用户所在的组:

此处用户是另外的当前的账号,所以去获取时,本身就2个,连一页20个都不到,所以page=2或更多页,则无法获取到,所以报错。

所以先去给 

组成员剧本列表

中,加上根据作者去筛选

然后从统计页面跳转过来时,自动选中该作者

然后翻页时,把当前选中的用户加上,应该就可以了。

【总结】

最后是加上对应的用户授权,即可正常翻页。

相关的部分代码改动:

  componentDidMount() {
    console.log(`GroupOwnerScriptList componentDidMount`)
    const { dispatch, currentUser, currentUserFunctionGroupList } = this.props;
    console.log(`currentUser=`, currentUser)
    console.log(`currentUserFunctionGroupList=`, currentUserFunctionGroupList)

    dispatch({
      type: 'user/fetchCurrent',
    }).then(() => {
    });

    const parsed = queryString.parse(this.props.location.search);
    console.log(`parsed=`, parsed)

    let authorId = ""

    if (parsed) {
      authorId = parsed.author_id;
      console.log(`authorId=`, authorId)

      if (authorId){
        const queryParamDict = {
          author_id: authorId,
        };
        console.log(`queryParamDict=`, queryParamDict)

        dispatch({
          type: 'script/fetchGroupOnwerScripts',
          payload: queryParamDict,
        });
      } else {
        dispatch({
          type: 'script/fetchGroupOnwerScripts',
        });
      }
    }

    const curUserName = currentUser.name
    let curFunctionGroup = {}
    console.log(`curUserName=`, curUserName)
    for (const curGroup of currentUserFunctionGroupList){
      console.log(`curGroup=`, curGroup)
      if (curGroup.owner === curUserName){
        curFunctionGroup = curGroup
        console.log(`found curFunctionGroup=`, curFunctionGroup)
        this.setState({currentOwnerFunctionGroup: curFunctionGroup})
        break
      }
    }

    if (curFunctionGroup) {
      const functionGroupID = curFunctionGroup.id;
      console.log(`functionGroupID=`, functionGroupID)
      if (functionGroupID) {
        dispatch({
          type: 'functionGroup/fetchFunctionGroupAndMembers',
          payload: functionGroupID,
        }).then((respGroupAndMembers) => {
          console.log(`fetchFunctionGroupAndMembers response`)
          console.log(`respGroupAndMembers=`, respGroupAndMembers)
          console.log(`this.props=`, this.props)
          const { functionGroup: { functionGroup, members } } = this.props;
          // const { functionGroup, members } = this.props;
          console.log(`functionGroup=`, functionGroup)
          console.log(`members=`, members)

          if (members) {
            const membersList = members.results
            console.log(`membersList=`, membersList)

            console.log(`before setFieldsValue authorId=`, authorId)

            const { form } = this.props;
            form.setFieldsValue({
              author_id: authorId || "",
            });

            const { formValues } = this.state;
            console.log(`formValues=`, formValues)
            const newFormValues = {
              author_id: authorId || "",
              ...formValues,
            };
            console.log(`newFormValues=`, newFormValues)
            this.setState({
              formValues: newFormValues,
            });

            this.setState({
              currentOwnerFunctionGroupMembers: membersList || [],
            });
          }
        });
      }
    };

src/common/router.js

    '/script/group-owner-script-list': {
      component: dynamicWrapper(app, ['script', 'user', 'functionGroup'], () => import('../routes/Script/GroupOwnerScriptList')),
    },

    '/script/group-owner-script-count': {
      component: dynamicWrapper(app, ['script', 'user'], () => import('../routes/Script/GroupOwnerScriptCount')),
    },

效果:

转载请注明:在路上 » 【已解决】Antd Pro中翻页出错:发出的请求针对的是不存在的记录

发表我的评论
取消评论

表情

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址
60 queries in 0.127 seconds, using 18.88MB memory