Why when I call a method within a middleware does not return an HttpResponse?

32 views
Skip to first unread message

Neto

unread,
Apr 20, 2016, 10:43:21 AM4/20/16
to Django users
Hi,

I have a middleware that checks whether the user is logged in.

class Check(object):

"""
Check
"""

@staticmethod
def process_request(request):

def is_authenticated_user():

try:
request.session['user']
except KeyError:
return redirect(reverse('mysite:login'))

        is_authenticated_user()

Why it does not works? How to solve?

술욱

unread,
Apr 20, 2016, 1:58:03 PM4/20/16
to django...@googlegroups.com
What's the value of request.session['user'] when user is not logged in?

My guess is it's an instance of AnonymousUser.
> --
> You received this message because you are subscribed to the Google Groups
> "Django users" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to django-users...@googlegroups.com.
> To post to this group, send email to django...@googlegroups.com.
> Visit this group at https://groups.google.com/group/django-users.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/django-users/7a9effe3-9fd3-42ff-bfd3-5ab7ea03030e%40googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.

Neto

unread,
Apr 20, 2016, 2:10:25 PM4/20/16
to Django users
The problem is that when happens the exception, the redirect does not work, only if I put return before is_authenticated_user(), like this:


class Check(object):

"""
Check
"""

@staticmethod
def process_request(request):

def is_authenticated_user():

try:
request.session['user']
except KeyError:
return redirect(reverse('mysite:login'))

        return is_authenticated_user()  # why I need put "return" here?

술욱

unread,
Apr 20, 2016, 3:15:10 PM4/20/16
to django...@googlegroups.com
because from the exception you return to process_request() and if you
don't return anything None in implied.
> https://groups.google.com/d/msgid/django-users/1c984bab-f08f-4355-8aa2-c6e28b5ee579%40googlegroups.com.
Reply all
Reply to author
Forward
0 new messages