57👍
There are 3 things I can think of off the top of my head:
- Just used named urls, it’s more robust and maintainable anyway
-
Try using
django.core.urlresolvers.reverse
at the command line for a (possibly) better error>>> from django.core.urlresolvers import reverse >>> reverse('products.views.filter_by_led')
-
Check to see if you have more than one url that points to that view
37👍
Shell calls to reverse (as mentioned above) are very good to debug these problems, but there are two critical conditions:
- you must supply arguments that matches whatever arguments the view needs,
- these arguments must match regexp patterns.
Yes, it’s logical. Yes, it’s also confusing because reverse will only throw the exception and won’t give you any further hints.
An example of URL pattern:
url(r'^cookies/(?P<hostname>[^/]+)/(?P<url_id>\d+)/$', 'register_site.views.show_cookies', name='show_cookies'),
And then what happens in shell:
>>> from register_site.views import show_cookies
>>> reverse(show_cookies)
NoReverseMatch: Reverse for 'register_site.views.show_cookies' with arguments '()' and keyword arguments '{}' not found.
It doesn’t work because I supplied no arguments.
>>> reverse('show_cookies', kwargs={'url_id':123,'hostname': 'aaa'})
'/cookies/aaa/123'
Now it worked, but…
>>> reverse('show_cookies', kwargs={'url_id':'x','hostname': 'www.dupa.com'})
NoReverseMatch: Reverse for 'show_cookies' with arguments '()' and keyword arguments '{'url_id': 'x', 'hostname': 'www.dupa.com'}' not found.
Now it didn’t work because url_id didn’t match the regexp (expected numeric, supplied string).
You can use reverse with both positional arguments and keyword arguments. The syntax is:
reverse(viewname, urlconf=None, args=None, kwargs=None, prefix=None, current_app=None)
As it comes to the url template tag, there’s funny thing about it. Django documentation gives example of using quoted view name:
{% url ‘news.views.year_archive’ yearvar %}
So I used it in a similar way in my HTML template:
{% url ‘show_cookies’ hostname=u.hostname url_id=u.pk %}
But this didn’t work for me. But the exception message gave me a hint of what could be wrong – note the double single quotes around view name:
Reverse for ”show_cookies” with arguments…
It started to work when I removed the quotes:
{% url show_cookies hostname=u.hostname url_id=u.pk %}
And this is confusing.
- [Django]-A Better Django Admin ManyToMany Field Widget
- [Django]-Mac OS X – EnvironmentError: mysql_config not found
- [Django]-Meaning of leading underscore in list of tuples used to define choice fields?
12👍
You need single quotes around the view name
{% url 'viewname' %}
instead of
{% url viewname %}
- [Django]-Django not sending emails to admins
- [Django]-How to expire Django session in 5minutes?
- [Django]-Error: No module named psycopg2.extensions
4👍
I had a similar problem and the solution was in the right use of the ‘$’ (end-of-string) character:
My main url.py looked like this (notice the $ character):
urlpatterns = [
url(r'^admin/', include(admin.site.urls )),
url(r'^$', include('card_purchase.urls' )),
]
and my url.py for my card_purchases app said:
urlpatterns = [
url(r'^$', views.index, name='index'),
url(r'^purchase/$', views.purchase_detail, name='purchase')
]
I used the ‘$’ twice. So a simple change worked:
urlpatterns = [
url(r'^admin/', include(admin.site.urls )),
url(r'^cp/', include('card_purchase.urls' )),
]
Notice the change in the second url! My url.py for my card_purchases app looks like this:
urlpatterns = [
url(r'^$', views.index, name='index'),
url(r'^purchase/$', views.purchase_detail, name='purchase')
]
Apart from this, I can confirm that quotes around named urls are crucial!
- [Django]-How to hide some fields in django-admin?
- [Django]-How to expire session due to inactivity in Django?
- [Django]-Django template system, calling a function inside a model
2👍
In case it helps someone, I had a similar issue and the error was because of two reasons:
-
Not using the app’s namespace before the url name
{% url 'app_name:url_name' %}
-
Missing single quotes around the url name (as pointed out here by Charlie)
- [Django]-Unique fields that allow nulls in Django
- [Django]-Running django tutorial tests fail – No module named polls.tests
- [Django]-Auto-create primary key used when not defining a primary key type warning in Django
0👍
I don’t think you need the trailing slash in the URL entry. Ie, put this instead:
(r'^led-tv$', filter_by_led ),
This is assuming you have trailing slashes enabled, which is the default.
- [Django]-Django URLS, how to map root to app?
- [Django]-Is there a way to negate a boolean returned to variable?
- [Django]-Unique BooleanField value in Django?
- [Django]-Django Generic Views using decorator login_required
- [Django]-Django: TemplateSyntaxError: Could not parse the remainder
- [Django]-'EntryPoints' object has no attribute 'get' – Digital ocean