Why does the login_required decorator return a 302 status code?

I have a view that allows me to add tags dynamically using an ajax request. It looks like this:

def addtag(request):
   a view to create a new tag in the tag database
   some logic here


This is what my url.py looks like:

urlpatterns = patterns('',
                       url(r'^addtag/$',addtag, name='addtag'),


And my test does this:

def test_addtag(self):

    response = self.client.post(url,{'addtag':'"new tag"'})


I expected the returned status code to be 401 since testclient will not log in. So the first decorator, checking if the request passed, is sent happily. Then I expected the login_required decorator to return 401, but it didn't:

AssertionError: 302 != 401


At first I thought that the login_required decorator would be redirected to some login page. Checked that I don't have a pointer settings.LOGIN_REDIRECT_URL

. So what does login_required do in this case?


source to share

1 answer


is redirected to the login page if the user is not logged in - so the view returns 302 in that case. (If you haven't installed LOGIN_REDIRECT_URL

it, it uses the default.)



All Articles