Django在基于类的视图中访问request.user

时间:2016-08-10 23:01:01

标签: python django

我目前正在处理以下问题:用户可以访问页面test.com/BlogPostTitle。其中BlogPostTitle是一个slu ..如果存在具有拟合标题的博客帖子,Django应该呈现所述博客文章的DetailView。如果它不存在,Django应该呈现一个表单来创建博客文章。

到目前为止这是有效的:

class EntryDetail(DetailView): # Displays blog entry, if it exists
    model = Blog
    slug_field = 'title'
    template_name = 'app/entry.html'    

class EntryForm(FormView): # Displays form, if entry 404s
    template_name = 'app/create.html'
    form_class = EntryForm
    success_url = '/'

    def form_valid(self, form):
        form.save()
        return super(EntryForm, self).form_valid(form)

class EntryDisplay(View):
    def get(self, request, *args, **kwargs):
        try:
            view = EntryDetail.as_view()
            return view(request, *args, **kwargs)
        except Http404:
            if check_user_editor(self.request.user) == True: # Fails here
                view = EntryForm.as_view()
                return view(request, *args, **kwargs)
            else:
                pass

现在,只有组中的用户"编辑"应该能够看到表格/创建帖子:

def check_user_editor(user):
    if user:
        return user.groups.filter(name="editor").exists() # Returns true, if user in editor group
    else:
        return False⋅ 

如您所见,我已在EntryDisplay中实现了该功能,但Django错误'User' object is not iterable

我猜测我与SingleObjectMixin合作,但我还没有完全理解这方面的文档。

非常感谢任何帮助。

完整追溯:

回溯:

File "/home/django/local/lib/python3.4/site-packages/django/views/generic/detail.py" in get_object
  53.             obj = queryset.get()

File "/home/django/local/lib/python3.4/site-packages/django/db/models/query.py" in get
  385.                 self.model._meta.object_name

During handling of the above exception (Blog matching query does not exist.), another exception occurred:

File "/home/django/mediwiki/mediwiki/views.py" in get
  68.             return view(request, *args, **kwargs)

File "/home/django/local/lib/python3.4/site-packages/django/views/generic/base.py" in view
  68.             return self.dispatch(request, *args, **kwargs)

File "/home/django/local/lib/python3.4/site-packages/django/views/generic/base.py" in dispatch
  88.         return handler(request, *args, **kwargs)

File "/home/django/local/lib/python3.4/site-packages/django/views/generic/detail.py" in get
  115.         self.object = self.get_object()

File "/home/django/local/lib/python3.4/site-packages/django/views/generic/detail.py" in get_object
  56.                           {'verbose_name': queryset.model._meta.verbose_name})

During handling of the above exception (No blog found matching the query), another exception occurred:

File "/home/django/local/lib/python3.4/site-packages/django/core/handlers/exception.py" in inner
  39.             response = get_response(request)

File "/home/django/local/lib/python3.4/site-packages/django/core/handlers/base.py" in _legacy_get_response
  249.             response = self._get_response(request)

File "/home/django/local/lib/python3.4/site-packages/django/core/handlers/base.py" in _get_response
  187.                 response = self.process_exception_by_middleware(e, request)

File "/home/django/local/lib/python3.4/site-packages/django/core/handlers/base.py" in _get_response
  185.                 response = wrapped_callback(request, *callback_args, **callback_kwargs)

File "/home/django/local/lib/python3.4/site-packages/django/views/generic/base.py" in view
  68.             return self.dispatch(request, *args, **kwargs)

File "/home/django/local/lib/python3.4/site-packages/django/views/generic/base.py" in dispatch
  88.         return handler(request, *args, **kwargs)

File "/home/django/mediwiki/mediwiki/views.py" in get
  74.                 view = HttpResponse(request.user)

File "/home/django/local/lib/python3.4/site-packages/django/http/response.py" in __init__
  293.         self.content = content

File "/home/django/local/lib/python3.4/site-packages/django/http/response.py" in content
  319.             content = b''.join(self.make_bytes(chunk) for chunk in value)

File "/home/django/local/lib/python3.4/site-packages/django/utils/functional.py" in inner
  235.         return func(self._wrapped, *args)

Exception Type: TypeError at /test
Exception Value: 'User' object is not iterable

2 个答案:

答案 0 :(得分:0)

模板渲染时是否发生错误?如果是这样的话,我想你已经在单个User对象上创建了属性。我想你可能需要user.values()

BTW,check_user_editor应该更简单:

def check_user_editor(user):
    return user.groups.filter(name="editor").exists()

答案 1 :(得分:0)

您的错误位于mediwiki.views中的第74行:

view = HttpResponse(request.user)

HttpResponse需要一个字符串或一个可迭代的。由于request.user不是字符串,因此它会尝试将其用作可迭代,但失败。

如果没有实际代码我就不能说太多。如果实际上你想要只发送用户的字符串表示作为响应,则需要将其转换为字符串:

view = HttpResponse(str(request.user))