theCed7 theCed7 - 3 months ago 20
Python Question

Django access request.user in class based view

I'm currently working on the following issue: The user can access the page

test.com/BlogPostTitle
. Where
BlogPostTitle
is a slug. If a Blog post with the fitting title exists, Django should render the DetailView of said blog post. If it doesn't exist, Django should render a form to create a blog post.

This works so far:

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


Now, only users who are in the group "editor" should be able to see the form/create a post:

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


As you can see, I've implemented the function in the
EntryDisplay
, however, Django errors
'User' object is not iterable
.

I'm guessing I've to work with
SingleObjectMixin
, but I haven't quite understood the docs on that.

Any help would be much appreciated.

Full traceback:

Traceback:

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

Answer

Your error is in line 74 in mediwiki.views:

view = HttpResponse(request.user)

HttpResponse expects a string or an iterable. Since request.user is not a string, it tries to use it as an iterable, which fails.

I can't say much without the actual code. If in fact you want to send just a string representation of the user as the response, you need to cast is to a string:

view = HttpResponse(str(request.user))
Comments