Django newbie: try canceling auth.views.login in login_required () decorator

I am new to stackoverflow and django ...

A short question (see the “long question” below), the following code in the myapps / views.py file did not execute:

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

Mistake:

 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 refers to myproject / urls.py)

I suppose there is something like chicken and eggs here, but I can't figure out where I am going wrong. I am trying to set LOGIN_URL in settings.py, as with the same error:

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

Now a long question (what context, why I want to do this):

Working with django 1.3.1 I got the following view protected by auth.decorator:

 @login_required def my_view(request) pass 

This decorator redirects to / accounts / login by default (it works and works for me).

Using the development server, all URLs refer to localhost: /

On the production server (using wsgi), all the URLs refer to my server: / path1 / This is due to the apache configuration, which says something like: WSGIScriptAlias ​​/ path1 / var / www / path / to / script.wsgi

And that is good for me.

All URLs defined in myproject / urls.py automatically correspond to this new path, so thanks to django my whole site is working on this new "html root".

But my protected view is still being redirected to my server: / accounts / login / instead of o my-server: / path1 / accounts / login

so far i am working using settings.py

 LOGIN_URL=/path1/accounts/login/ 

or using the login_url parameter in the login_required window:

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

But I would like this login window to be relative to the entire site path without setting "path1" in apache and django / settings.py

I don't think using reverse in settings.py is the right thing. and also do not use it in the decorator. But so far I don’t know how to handle this ...

+7
source share
3 answers

You cannot use reverse in the login_required decorator or in settings.py , because the URL configuration was not loaded during the import of parameters / views.

In Django 1.4+, you can use reverse_lazy . In earlier versions of Django, you can use reverse_lazy or see my answer to the question of URL mapping in settings .

In Django 1.5+, the LOGIN_URL parameter accepts named url patterns. For example, if you name your login URL “login”, you can simply do:

 LOGIN_URL = 'login' 
+13
source

after the reverse () function, I will figure out another way to achieve my goal:

I am fine using standard URLs (accounts / login, etc.) I just want to add the root of the html document, which is not "/" in my case.

from my test, this does not work in settings.py (is it too early at this point?)

in my views.py:

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

to become

 from django.core.urlresolvers import get_script_prefix @login_required(login_url=get_script_prefix() + "accounts/login/") 
0
source

use reverse_lazy()

the docs say:

This is useful when you need to use a URL spread in front of your URLConf projects. Here are some common cases when this function is needed:

  • providing a return URL as the url attribute of the class’s general view.
  • providing the return URL for the decorator (for example, the login_url argument for the django.contrib.auth.decorators.permission_required () decorator).
  • providing the return URL as the default value for the parameter in the function signature.
0
source

All Articles