I am trying to understand why I am seeing a NoReverseMatch error when trying to use Django Contact Form.
The error occurs when I add a link using the following syntax to index.html:
<h3>Contact</h3>
If I use the following hardcoded syntax then no errors occur and the link to the contact form from index.html works as expected.
<h3>Contact</h3>
What I am trying to achieve is similar to what is shown the Django tutorial about removing hard coded urls.
The full error I see is:
NoReverseMatch at /index
Reverse for 'contact' with arguments '()' and keyword arguments '{}'
not found. 0 pattern(s) tried: []
In case of need, my abbreviated urls.py is:
urlpatterns = patterns('',
...
url(r'index$', views.index, name='index'),
...
url(r'^contact/', include('contact_form.urls')'),
)
I know I am missing something obvious!
It's because there is no url with name contact.
url(r'^contact/', include('contact_form.urls')'),
is url that will map all urls starting with contact to the contact_form.urls. Official docs don't say how to access contact view, but with basic understanding of django we can do something like this:
urlpatterns = patterns('',
...
url(r'index$', views.index, name='index'),
...
url(r'^contact/', include('contact_form.urls', namespace='contacts')),
)
and the in the template:
<h3>Contact</h3>
contact_form url name is found in the source code of the module.
when you use {% url 'contact' %} in template, the 'contact' is actually the name of route. In your url patterns there is no route with this name. You should include something like this into your contact_forms.urls.py:
url(r'$', views.index, name='contact_index')
Also, you should change "contact/" pattern to:
url(r'^contact/', include('contact_form.urls', namespace='contacts'))
and then use this when creating link in template:
{% url 'contacts:contact_index' %}
Related
strong textenter image description here
Reverse for 'auth_password_reset' not found. 'auth_password_reset' is not a valid view function or pattern name.
Please Tell me how to solve this error
The error is in the html file, You would have used auth_password_reset as a url, but django cannot find the the url hence it raises the error. More on noReverseMatch Here
Here are some possible errors:
You have not given any url the name 'auth_password_reset' hence django cannot find it.
If you're using any app_name you will have to include that as well.
For example:
urls.py
app_name = 'app_one'
urlpatterns=[
path('home/', views.auth_password_reset, name='auth_password_reset'),
]
html file:
{% url 'app_one:auth_password_reset' %}
I'm trying to use Django's built in authentication views for password reset, however I can't figure out why the application is bugging out for the built in authentication view password_reset_confirm. Any idea how I can fix this, or at least debug it? Been stuck on this issue for a while now.
Template error
NoReverseMatch at /accounts/password/reset/
Reverse for 'django.contrib.auth.views.password_reset_confirm' with arguments '()' and keyword arguments '{u'uidb64': u'xxxxxxxxxxxxxxxxxxxxxxx', u'token': u'xxxxxxxxxxxxxxxxx'}' not found.
Error during template rendering
In template /home/user/Envs/local/lib/python2.7/site-packages/django/contrib/admin/templates/registration/password_reset_email.html, error at line 6
Reverse for 'django.contrib.auth.views.password_reset_confirm' with arguments '()' and keyword arguments '{u'uidb64': u'xxxxxxxxxxxxxxxxxxxxxxx', u'token': u'xxxxxxxxxxxxxxxx'}' not found.
----> {{ protocol }}://{{ domain }}{% url 'django.contrib.auth.views.password_reset_confirm' uidb64=uid token=token %} <---- Template errors here
urls.py
from django.contrib.auth.views import login, password_reset, password_reset_confirm, password_reset_done, password_reset_complete
urlpatterns = patterns('userProfile.views',
url(r'^password/reset/(?P<uidb64>[0-9A-Za-z_\-]+)/(?P<token>.+)/$',
password_reset_confirm,
name='password_reset_confirm'),
...)
Attempted Solutions
Per Joseph's suggestion, modifying the admin template fixes the problem. BUT, why can't I reverse the built in auth view???
{{ protocol }}://{{ domain }}{% url 'userProfile:password_reset_confirm' uidb64=uid token=token %}
I believe since you've given the URL mapping a name, you can use just that name in the reversing:
{% url 'password_reset_confirm' uid token %}
Assuming uid and token are in the context.
You might be able to do it the way you want to like this:
urlpatterns = patterns(
'django.contrib.auth.views',
(r'^password/reset/(?P<uidb64>[0-9A-Za-z_\-]+)/(?P<token>.+)/$', 'password_reset_confirm'),
)
Once you define a name though, I believe that's what you have to use for reversing it. Since your URL is not a tuple of string,string but of string,function,string, you can't reverse on the name of the view function, but on the name of the URL pattern (that last string).
I may be mistaken here though.
[Second update]
The first argument in patterns is a prefix. You could try doing this instead:
from django.contrib.auth.views import login, password_reset, password_reset_confirm, password_reset_done, password_reset_complete
urlpatterns = patterns('',
url(r'^password/reset/(?P<uidb64>[0-9A-Za-z_\-]+)/(?P<token>.+)/$','django.contrib.auth.views.password_reset_confirm'),
url(r'...other_app_urls_here','other_view'),
...)
Or if you really want to keep the prefix for your app, just add the patterns seperately:
from django.contrib.auth.views import login, password_reset, password_reset_confirm, password_reset_done, password_reset_complete
urlpatterns = patterns('',
url(r'^password/reset/(?P<uidb64>[0-9A-Za-z_\-]+)/(?P<token>.+)/$','django.contrib.auth.views.password_reset_confirm'),
)
urlpatterns += patterns('userProfile.views',
...userProfile_urls_here...
)
Either of those two solutions should allow you to reverse the whole builtin view.
Doc links:
https://docs.djangoproject.com/en/1.7/ref/urls/#django.conf.urls.patterns
https://docs.djangoproject.com/en/1.7/topics/http/urls/#urlpatterns-view-prefix
https://docs.djangoproject.com/en/1.7/topics/http/urls/#multiple-view-prefixes
My Django app's site-wide urls.py file looks like this:
urlpatterns = patterns('',
url(r'^$', include('myApp.urls')),
url(r'^admin/', include(admin.site.urls)),
url (
r'^accounts/register/$',
RegistrationView.as_view(form_class=extendedRegistrationForm),
),
url(r'^accounts/', include('registration.backends.default.urls')),
url(r'^', include('myApp.urls')),
)
I also have a urls.py specific to myApp but I have not shown that here because I don't think it's relevant.
In my template file, I have this:
{% url logout %}
It gives me this error:
'url' requires a non-empty first argument. The syntax changed in Django 1.5, see the docs.
When I change the template to:
{% url 'logout' %}
It gives me the following error:
Reverse for 'logout' with arguments '()' and keyword arguments '{}' not found. 0 pattern(s) tried: []
How do I put a link to logout in my view?
Add a logout url like this:
url(r"^logout/$", "django.contrib.auth.views.logout_then_login",
name="logout"),
The correct template syntax is:
{% url 'logout' %}
You didn't include django.contrib.auth's URLs in your urls.py, so there is no logout URL.
Try adding something like
url(r'^auth/', include('django.contrib.auth.urls')),
In django 3.0 {% url 'admin:logout' %} works perfect.
I'm trying make hyperlink to next page. But I get error.
Reverse for 'show_units_list' with arguments '()' and keyword arguments '{}' not found.
Please check my code.
view.py
def units_list(request):
units_list = unit.objects.all()
return render_to_response(
'portal/units.html',
{'units': units_list},
context_instance=RequestContext(request)
)
urls.py
urlpatterns = patterns('',
# Main web portal entrance.
(r'^$', views.portal_main_page),
url(r'^portal/', views.units_list, name='show_units_list'),
)
html
<a href="?page={% url show_units_list %}">
Thanks
You forget app_name,
url(r'^portal/', app_name.views.units_list, name='show_units_list'),
units_list is part an app "portal"
I got this error, but can't seem to figure it out. I copied it directly from a previous Django project, hence part of the confusion.
TemplateSyntaxError at Caught NoReverseMatch while rendering: Reverse for 'about' with arguments '()' and keyword arguments '{}' not found.
In my index.html, I have a link to {% url about %} didn't link to the about.html template
Urls.py has this:
urlpatterns = patterns('django.views.generic.simple',
url(r'^about/$', 'direct_to_template', {"template":"about.html"}, name="about"),
)
The problem was my second urlpattern was overriding the first pattern.
Instead of:
urlpatterns = patterns('',
it needed to be:
urlpatterns += patterns('',
The url regex is expecting an ending slash. Does the offending url end with a slash?
If you do have a PREPEND_SLASHES settings differing from your last projects, that might explain the error you're seeing!
Your url is ok. You need to check two things:
Is the urls.py included from the main urls.py?
Is the application added to INSTALLED_APPLICATIONS in settings.py?