segunchrist segunchrist - 1 year ago 523
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 Source
{% 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 You need to provide a name for login like you have for home and then use that.

Correct way is: ->

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

template ->

<form method="post" action="{% url 'login' %}">
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download