gpt4 book ai didi

python - Django ValueError : The view usermanager. views.group_perm 没有返回 HttpResponse 对象。它返回 None 而不是

转载 作者:行者123 更新时间:2023-12-05 07:09:51 25 4
gpt4 key购买 nike

我正在创建一个新闻网站。当我尝试从组中删除权限时,显示相同的错误。这个函数上面的代码几乎是一样的。但他们工作正常。请帮忙

这是我的 View 文件def group_perm(请求,名称):

if not request.user.is_authenticated:
return redirect('my_login')


perm = 0

for i in request.user.groups.all():
if i.name == "masteruser": perm = 1

if perm == 0:
error = "Access Denied"
return render(request, 'back/error.html',{'error': error})


permission = Permission.objects.all()


for group in Group.objects.filter(name=name):
gperm = group.permissions.all()


return render(request, 'back/group_perm.html', {'gperm':gperm, 'name':name, 'permission': permission})

def group_permission_del(request, gname, name):

if not request.user.is_authenticated:
return redirect('my_login')


perm = 0
#"request.user" means current logged User
for i in request.user.groups.all():
if i.name == "masteruser": perm = 1

if perm == 0:
error = "Access Denied"
return render(request, 'back/error.html',{'error': error})



group = Group.objects.get(name=gname)

gperm = Permission.objects.get(codename=name)


group.permissions.remove(gperm)

return redirect('manage_permission')

最佳答案

在这里,您的 return 语句位于 group_perm 函数的 for 循环内

Whitespace is used to denote blocks. like other languages using curly brackets { , }.

def group_perm(request, name):
for i in request.user.groups.all():
if i.name == "masteruser": perm = 1

if perm == 0:
error = "Access Denied"
return render(request, 'back/error.html', {'error': error})

permission = Permission.objects.all()

for group in Group.objects.filter(name=name):
gperm = group.permissions.all()

return render(request, 'back/group_perm.html', {'gperm': gperm, 'name': name, 'permission': permission})

编辑:1

Django ValueError: The view usermanager.views.group_perm didn't return an HttpResponse object. It returned None instead

这个错误的答案是你的 return 语句在 for 循环内,因此,当你调用你的函数时,它返回 None。是这样解决的吗?

现在你的另一个函数是这样的:

def group_permission_del(request, gname, name):
if not request.user.is_authenticated:
return redirect('my_login')

perm = 0
# "request.user" means current logged User
for i in request.user.groups.all():
if i.name == "masteruser": perm = 1

if perm == 0:
error = "Access Denied"
return render(request, 'back/error.html', {'error': error})

group = Group.objects.get(name=gname)
gperm = Permission.objects.get(codename=name)
group.permissions.remove(gperm)

return redirect('manage_permission')

关于python - Django ValueError : The view usermanager. views.group_perm 没有返回 HttpResponse 对象。它返回 None 而不是,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61437459/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com