segunchrist segunchrist - 3 months ago 63
Python Question

No Reverse Match with Django Auth Views

I created a custom User model following the example in the Django documentation, now I'm trying to use Django auth views but I keep getting

NoReverseMatch at /accounts/login/


Reverse for 'django.contrib.auth.views.login' with arguments '()' and keyword arguments '{}' not found. 0 pattern(s) tried: []


This is the url conf:

from django.contrib.auth import views as auth_views

urlpatterns = [
url(r'^accounts/login/$', auth_views.login, {'template_name': 'login.html',
'authentication_form': LoginForm}),
url(r'^logout/$', auth_views.logout, {'next_page': '/accounts/login'}),
url(r'^$', home, name="home"),
]


And I have this line in my template:

<form method="post" action="{% url 'django.contrib.auth.views.login' %}">

Answer
{% url 'django.contrib.auth.views.login' %}

This is incorrect. We put the name given to the url here instead of the location of the view.

Please see https://docs.djangoproject.com/en/1.10/ref/templates/builtins/#url. You need to provide a name for login like you have for home and then use that.

Correct way is: urls.py ->

url(r'^accounts/login/$', auth_views.login, {'template_name': 'login.html','authentication_form': LoginForm}, name="login") ,

template ->

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