Django Caught NoReverseMatch - TemplateSyntaxError - django

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?

Related

NoReverseMatch at /accounts/login/ in django ()

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' %}

NoReverseMatch at /admin/ Reverse for 'logout' with no arguments not found. 1 pattern(s) tried: ['$admin/logout\\/$']

my code for MainProjects.urls is
from django.conf.urls import url
from django.contrib import admin
from django.conf.urls import include
admin.autodiscover()
urlpatterns = [
url(r'^$',include('register_complaint.urls')),
url(r'^register/',include('register_complaint.urls')),
url(r'^admin/$', admin.site.urls),
]
I have tried removing '$' from 'admin' still the same error occurs.
Request URL: http://localhost:8000/admin/
Django Version: 2.0.6
Exception Type: NoReverseMatch
Exception Value:
Reverse for 'logout' with no arguments not found. 1 pattern(s) tried: ['$admin/logout\/$']
You must remove the $ from the regexp because you we are appending the included urls to the end of the regular expression.
The $ in a regular expression represents the end of the string, and it would make sense in an urlpattern that points directly in a view. but a urlpattern that includes another urlpattern is supposed to read only the first part of the URL, because the remaining part is read by the included one. from this the need to begin with ^ and to not append the $.
url(r'^admin/', admin.site.urls)
and ensure 'django.contrib.admin' is added to the INSTALLED_APPS settings for your project. Apart from that, everything should work.

NoReverseMatch at /index

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' %}

Django URL kwargs both in main app and subapp, reverse fails

EDIT: Never mind, it was something completely unrelated (wrong URL name).
I have a Django urls.py that includes another urls.py from a subapp. I get "reverse not found" errors when trying to use reverse(). I use keyword arguments in the URL both before and after the include, it's basically:
First urls.py:
urlpatterns = patterns(
'',
# Change requests are in a subapp
url(r'^projects/(?P<project_slug>[^/]+)/changerequests/',
include('myapp.changerequests.urls')),
)
And in the subapp's urls.py:
urlpatterns = patterns(
'',
url(r'^detail/(?P<request_id>\d+)/$',
views.RequestDetailPage.as_view(),
name='changerequests_detail'),
)
Now I want to find an URL with something like
url = reverse('changerequests_detail', kwargs={
'project_slug': self.project.slug,
'request_id': str(self.id)})
So it uses two kwargs, that are spread out over both urls.pys. But the reverse fails to find an answer (Reverse for 'changerequests_main' with arguments '()' and keyword arguments '{u'project_slug': u'123-2013_monitoring_slibaanwas-hdsr', u'request_id': '2'}' not found.).
Is that the problem? Is spreading kwargs over urls files this way not possible?
Maybe error occurs because it's trying to reverse 'changerequests_main' URL, not 'changerequests_detail'.

NoReverseMatch Error

I keep getting this error for the django login system. Here is part of my urls.py:
(r'^contractManagement/login', 'django.contrib.auth.views.login', {'template_name': 'login.html'}),
The exact error I am getting:
Exception Type: NoReverseMatch
Exception Value: Reverse for ''django.contrib.auth.views.login'' with arguments '()' and keyword arguments '{}' not found.
I can't understand why i am getting this error. If you need anything else let me know.
You don't show where you are trying to reverse this URL, but it looks like you have double-quoted it. If you're using the url tag, note that you don't need quotes around the url name:
{% url django.contrib.auth.views.login %}
not
{% url 'django.contrib.auth.views.login' %}
You see that ''the.unknown.view'' is reported including too many qoutes.
It is because the quoted syntax will be valid in Django 1.5 and higher. For Django 1.3 or 1.4, you should activate the future behavior by this line in the template:
{% load url from future %}
which is valid also for Django 1.5.
Example for Django 1.5+
{% url "path.to.some.view" %}
Classic syntax for Django <= 1.4.x (without "future" command) is:
{% url path.to.some.view %}
I would give your url a name (in order to do that, you need to use the url method) Also you should add a trailing slash to all your urls, cause the django CommonMiddleware is going to be doing a 302 redirect on all your urls if you don't:
from django.conf.urls.defaults import *
urlpatterns = patterns('',
url(r'^contractManagement/login/', 'django.contrib.auth.views.login', {'template_name': 'login.html'}, name='contract_login'),
)
Then you can use reverse in your code, or url in your templates, and if you ever decide to change the actual url (ie: changedCotractManagement/login/), as long as the name is the same, your code will still be good.
in code:
from django.core.urlresolvers import reverse
reverse('contract_login')
in template:
{% url contract_login %}
Edit: per MrOodles