Render ('django.contrib.auth.views.login'), pointing to a different URL than {% url 'django.contrib.auth.views.login'%}

I am using Django 1.5.1 using the standard Django authentication system.

INSTALLED_APPS = ( 'django.contrib.auth', 'django.contrib.contenttypes', 'django.contrib.sessions', 'django.contrib.sites', 'django.contrib.messages', 'django.contrib.staticfiles', 'django.contrib.admin', 'django.contrib.markup', 'django.contrib.admindocs', 'flowcharts', 'south', 'helpdesk', ) 

Here is the template located in the / login registration:

 {% extends "base.html" %} {% block content %} {% if form.errors %} <p>Your username and password didn't match. Please try again.</p> {% endif %} <form method="post" action="{% url 'django.contrib.auth.views.login' %}"> {% csrf_token %} <table> <tr> <td>{{ form.username.label_tag }}</td> <td>{{ form.username }}</td> </tr> <tr> <td>{{ form.password.label_tag }}</td> <td>{{ form.password }}</td> </tr> </table> <input type="submit" value="login" /> <input type="hidden" name="next" value="{{ next }}" /> </form> {% endblock %} 

and in my urls.py file:

 urlpatterns = patterns('', url(r'^accounts/login/$', 'django.contrib.auth.views.login'), url(r'^accounts/logout/$', 'django.contrib.auth.views.logout' , {'next_page': '/accounts/login/'}), ) 

The form action is a mapping to '/ helpdesk / login /'

But when I run render ('django.contrib.auth.views.login') in the python shell, I get '/ accounts / login /', which is the URL that I want to point to the form. Let me know if any other project information is needed.

0
source share
2 answers

Do you want to use django authentication system:

Enter this in your urls.py:

 urlpatterns = patterns('', url(r'^accounts/', include('django.contrib.auth.urls')) ) 

In your registration /login.html:

 <form method="post" action="{% url login %}"> 

You want to provide next_url when you log out, so whenever you want to call logout:

 <a href="{% url logout %}?next=/accounts/login/">Logout</a> 

Check line 191 of the link you provided. They call /helpdesk/login/ as login , and therefore your {% url login %} points to /helpdesk/login .

You must have url(r'^helpdesk/', include('helpdesk.urls')) before url(r'^accounts/', include('django.contrib.auth.urls')) in urls.py Reorder these two URLs.

+1
source

this is what im uses and gets it right

 <li><a href="{% url django.contrib.auth.views.login %}?next={{request.path}}">Login</a></li> 

so i think you need to remove single quotes

-1
source

Source: https://habr.com/ru/post/1238834/


All Articles