我正面临这些错误
l = request.user.groups.values_list('name',flat=True)
Traceback (most recent call last):
File "<console>", line 1, in <module>
NameError: name 'request' is not defined
答案 0 :(得分:1)
NameError:name&#39; request&#39;未定义
似乎您正在使用未定义的变量request
,
例如,request
可以来自一个视图,例如
from django.views.generic import View
class MyView(View):
def get(self, request):
# here you can use the request object
user = request.user
user.groups... # do something with the user groups
我正在尝试在django中找到与特定用户相关联的群组名称
您也可以直接使用该用户而无需任何HttpRequest
对象
from django.contrib.auth.models import User
user = User.objects.get(username='ashish')
user.groups... # do something with the user groups