1

When visiting a url it returns a error mentioned in the question title.

The View itself is a ListView.

When i debuged the view, it only looped through the dispatcher function and return the following error without getting to queryset. Here's the code.

ListView

class MailingListView(ListView, LoginRequiredMixin):
    # model = MailingList
    template_name = 'mailinglist/mailing_list.html'

    def get_queryset(self):
        return MailingList.objects.filter(owner=self.request.user)

    def dispatch(self, request, *args, **kwargs):
        if not request.user.is_authenticated:
            return reverse('user:login')
        return super().dispatch(request, *args, **kwargs)

Model

class MailingList(models.Model):
    id = models.UUIDField(primary_key=True, default=uuid.uuid4(), editable=False)
    name = models.CharField(max_length=140)
    owner = models.ForeignKey(to=user_model,
                              on_delete=models.CASCADE)

    def __str__(self):
        return self.name

    def get_absolute_url(self):
        return reverse('mailinglist:manage_list', kwargs={'pk': self.id})

    def user_can_use_mailinglist(self, user):
        return user == self.owner

Traceback

> Internal Server Error: /mailinglist/ Traceback (most recent call
> last):   File
> "/venv/lib/python3.7/site-packages/django/core/handlers/exception.py",
> line 34, in inner
>     response = get_response(request)   File "/venv/lib/python3.7/site-packages/django/utils/deprecation.py",
> line 93, in __call__
>     response = self.process_response(request, response)   File /venv/lib/python3.7/site-packages/django/middleware/clickjacking.py",
> line 26, in process_response
>     if response.get('X-Frame-Options') is not None: AttributeError: 'str' object has no attribute 'get'
1
  • add your error traceback Commented Dec 8, 2019 at 3:39

1 Answer 1

4

reverse() returns a string. But you are supposed to return HTTP responses.

Change in dispatch in MailingListView.

from django.http import HttpResponseRedirect

return HttpResponseRedirect(reverse('user:login'))
Sign up to request clarification or add additional context in comments.

3 Comments

Thank you it helped. Would upvote if had enough rep.
You may accept it. This will help another developer to understand this solution works
This answer should be accepted man... I was blowing my brain what I was doing wrong in my class based view then I found this. Thanks bro!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.