"NoReverseMatch at" error with "django.contrib.auth.views.login" tests

2

I would like to authenticate users using django.contrib.auth.views.login .

I want that when the login is correct I send them to dashboard . I have the following codes:

In settings:

LOGIN_REDIRECT_URL = '/usuarios/dashboard/'

In Urls Application:

url(r'^usuarios/', include('Usuarios.urls', namespace='usuarios')),
url(r'^login/$','django.contrib.auth.views.login',{'template_name' : 'admin/login.html',},name='login'),

In Users / Urls:

url(r'^dashboard/$', BlankView.as_view(), name='index'),

In Users / View:

class BlankView(TemplateView):
template_name = '/templates/index.html'

At the time of testing I get the login form ok but when I accept it, it sends me the following error:

NoReverseMatch at /usuarios/dashboard/

Reverse for 'logout' with arguments '()' and keyword arguments '{}' not found. 0 pattern(s) tried: []

Request Method:     GET
Request URL:    http://localhost/usuarios/dashboard/
Django Version:     1.9
Exception Type:     NoReverseMatch
Exception Value:    

Reverse for 'logout' with arguments '()' and keyword arguments '{}' not found. 0 pattern(s) tried: []
    
asked by Rocke 19.01.2016 в 13:06
source

2 answers

2

In django 1.9 it is used in the pattern urls.py:

url(r'^login/$', views.login, {'template_name': 'login.html'}, name='login'),
url(r'^logout/$', views.logout, {'next_page': '/login/'}, name='logout'),

in logout you redirect it to the page you want with netx_page.

In settings.py put the urls you want to redirect to too:

LOGIN_REDIRECT_URL = '/index/'
LOGIN_URL = '/login/'

In login.html you can close session with something like this:

<p>Estas logueado con el usuario {{ user.username|capfirst }}, <a href="{% url 'logout' %}">cerrar sesion</a></p>

This is deprecated:

    url(r'^login/$', 'django.contrib.auth.views.login', {'template_name' : 'admin/login.html'}, name='login'),
    url(r'^logout/$', 'django.contrib.auth.views.logout', {'next_page': '/login/'}, name='logout'),
    
answered by 20.02.2016 в 11:50
0

Most likely in the dashboard template you have some link to exit the system ( logout ), be sure to define the route correctly in your URLs:

urlpatterns = [
    # ...
    url(r'^login/$', 'django.contrib.auth.views.login', {'template_name' : 'admin/login.html'}),
    url(r'^logout/$', 'django.contrib.auth.views.logout', {'next_page': '/login/'}),
    # ...
]

With this the redirection should work well:

>>> from django.core.urlresolvers import reverse
>>> reverse('django.contrib.auth.views.login')
'/login/'
>>> reverse('django.contrib.auth.views.logout')
'/logout/'

Also, you can use name in your URLs :

urlpatterns = [
    # ...
    url(r'^login/$', 'django.contrib.auth.views.login', {'template_name' : 'admin/login.html'}, name='login'),
    url(r'^logout/$', 'django.contrib.auth.views.logout', {'next_page': '/login/'}, name='logout'),
    # ...
]

And now you can use reverse using the name parameter of your URLs:

>>> from django.core.urlresolvers import reverse
>>> reverse('login')
'/login/'
>>> reverse('logout')
'/logout/'

I think the error is occurring because in the template you should be using something like this:

<a href="{% url 'logout' %}">Salir</a>

With the last solution I should not give you problems anymore.

    
answered by 19.01.2016 в 13:39