TypeError:“ str”对象不可调用python3

时间:2018-07-16 12:52:12

标签: django python-3.x

def SaveRoles(request):
    if 'user_name' in request.session:
        if request.method == "POST":
            user_roles = request.POST['user_roles']
            user_permission_level = request.POST['user_permission_level']
            user_role_code = request.POST['user_role_code']
            code = user_roles(user_roles=user_roles, user_permission_level=user_permission_level, user_role_code=user_role_code)
            print("qqqqqq", code)
            try:
                model_status = code.save()
            except Exception as e:
                print("exception occured", e)
                return render(request, 'administrator/add_role.html', {'message': 'Oops some error occured'})
        if model_status == None:
            role = user_roles.objects.all()

            return HttpResponseRedirect('http://'+request.get_host()+'/admin/role-list/',{'message': 'Role added successfully','code': code})
        else:
            return HttpResponseRedirect('http://'+request.get_host()+'/admin/role-list/')
    else:
        return HttpResponseRedirect('/admin/')

当我运行此功能时,我得到了

  

TypeError:“ str”对象不可调用

我搜索了所有代码,甚至在项目的任何地方都没有使用str这个词 但是我找不到错误

1 个答案:

答案 0 :(得分:1)

user_roles是一个字符串,因为您要分配request.POST['user_roles']的值。您可能需要进行更改。

user_roles = request.POST['user_roles']

user_roles_val = request.POST['user_roles']

例如:

def SaveRoles(request):
    if 'user_name' in request.session:
        if request.method == "POST":
            user_roles_val = request.POST['user_roles']    #Update
            user_permission_level = request.POST['user_permission_level']
            user_role_code = request.POST['user_role_code']
            code = user_roles(user_roles=user_roles_val, user_permission_level=user_permission_level, user_role_code=user_role_code)   #Update
            print("qqqqqq", code)
            try:
                model_status = code.save()
            except Exception as e:
                print("exception occured", e)
                return render(request, 'administrator/add_role.html', {'message': 'Oops some error occured'})
        if model_status == None:
            role = user_roles.objects.all()

            return HttpResponseRedirect('http://'+request.get_host()+'/admin/role-list/',{'message': 'Role added successfully','code': code})
        else:
            return HttpResponseRedirect('http://'+request.get_host()+'/admin/role-list/')
    else:
        return HttpResponseRedirect('/admin/')