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:

@require_POST
@login_required
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):
    url='^addtag/$'

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

      

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?

+3


source to share


1 answer


@login_required

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.)



+4


source







All Articles