Mar 28, 2012

Login URLs in Django

Django is a pain.

I know - its a great framework. Its also supposed to be robust and highly recommended by a few friends when I asked around for what should I use as I wanted to learn how to develop a website.

I have been dilly-dallying with making the website for quite some time, but now I am trying to put in a few hrs every week into it. I enjoy it so far... but then I run into some stupid roadblock.

Today I ran into another one. If I do not document it, I am sure that I will make the same mistakes again.

Today's problem was with the login redirect URL.

I have a function with the decorator as follows :

@login_required
def foo(request, template_name="foo.html"):
    return render_to_response(template_name,locals(),context_instance=RequestContext(request))

I changed the default account redirect to /account so my urls.py has the following:
    (r'^account/', include('account.urls')),

Now, when I tried to click on the link which requires the login ( and I am not logged in) I got the following error :

The current URL, accounts/login/, didn't match any of these.

See the s in accounts. I tried searching everywhere where I had the accounts word. grep did not turn up anything and I was quite frustrated.

Finally, I reasoned that it has to be something with the way django does anything and started reading the settings documentation. Sure enough, @login_required requires a url which defaults to /accounts/login !

Why the Django documentation has to be so vague with login is something I don't understand.

So, I added the following lines in the settings.py :
LOGIN_URL='/account/login/'

Not it works !

Gah !

No comments:

Post a Comment