Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

django newbie: try to reverse auth.views.login in login_required() decorator

I'm new to stackoverflow and to django...

Brief question (see below for "long question"), the following code in myapps/views.py failed:

from django.core.urlresolvers import reverse
...
@login_required(login_url=reverse('django.contrib.auth.views.login'))
def my_view(request):
   pass

The error is:

ViewDoesNotExist at /
tried my-view2 in module myproject.myapps.views. Error was: 'module' object has no attribute 'my-view2'

'my-view2' is defined in myapps/views.py after my-view (and is referenced in myproject/urls.py)

I guess there is something like chicken and egg here but I can't figure out where I'm wrong. I try to set up LOGIN_URL in settings.py like that with the same error:

from django.core.urlresolvers import reverse
...
LOGIN_URL=reverse('django.contrib.auth.views.login')

Now long question (what context, why I want do that):

working with django 1.3.1 I got the following view, protected with auth.decorator:

@login_required
def my_view(request)
  pass

This decorator default redirect to /accounts/login (it works and that fine for me).

Using developpement server the whole urls are relative to localhost:/

In production server (using wsgi), the whole urls are relative to my-server:/path1/ This is due to the apache configuration that say something like: WSGIScriptAlias /path1 /var/www/path/to/script.wsgi

And that's fine for me.

All urls defined in myproject/urls.py are automagically relative to this new path, so thanks to django, all my site is working on this new "html root".

But my protected view still redirect to my-server:/accounts/login/ instead o my-server:/path1/accounts/login

so far I make it work using settings.py

LOGIN_URL=/path1/accounts/login/

or using login_url parameter of "login_required" decorator:

@login_required(login_url="/path1/accounts/login/")

But I would like that this login view be relative to the whole site path without configuring "path1" in both apache and django/settings.py

I don't feel that using reverse in settings.py is the right thing to do nor using it in a view decorator. But so far I don't know how to handle this...

like image 890
rhn Avatar asked Jan 09 '12 15:01

rhn


1 Answers

You can't use reverse in the login_required decorator or in settings.py, because the URL config hasn't been loaded at the time the settings/views are imported.

In Django 1.4+, you can use reverse_lazy. On earlier versions of Django, you could backport reverse_lazy, or see my answer to the question reverse url mapping in settings.

In Django 1.5+, the LOGIN_URL setting accepts named url patterns. For example, if you name your login URL pattern 'login', then you can simply do:

LOGIN_URL = 'login' 
like image 77
Alasdair Avatar answered Oct 06 '22 23:10

Alasdair



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!