NoReverseMatch Django URL

There is currently a NoReverseMatch error with the Django URL tag. The final guide to Django followed, the redemption of Django, and searched here and on the Internet.

URLs:

url(r'^test/', Search_Page), url(r'^search/', Search), url(r'^details/', Details_Main), url(r'^Link/(d+)/$', Link), url(r'^$', 'Parks.views.Link', name="home"), url(r'^(?P<result_name>)/$', Link), 

Views:

 def Link(request, result_name): return render_to_response('Search_Page.html') 

template:

 {% for result in results %} <a href="{% url name result.name %}">test</a> 

Error:

 NoReverseMatch at /search/ Reverse for 'name' with arguments '(u'North West Thrill Centre',)' and keyword arguments '{}' not found.Request Method: GET Request URL: http://127.0.0.1:8000/search/?search=a&type=parks&submit=Search Django Version: 1.4.2 Exception Type: NoReverseMatch Exception Value: Reverse for 'name' with arguments '(u'North West Thrill Centre',)' and keyword arguments '{}' not found. Exception Location: C:\Python27\lib\site-packages\django\template\defaulttags.py in render, line 424 Python Executable: C:\Python27\python.exe Python Version: 2.7.3 Python Path: ['C:\\Users\\User\\Documents\\Django\\ParkManager', 'C:\\Windows\\system32\\python27.zip', 'C:\\Python27\\DLLs', 'C:\\Python27\\lib', 'C:\\Python27\\lib\\plat-win', 'C:\\Python27\\lib\\lib-tk', 'C:\\Python27', 'C:\\Python27\\lib\\site-packages'] Server time: Mon, 4 Feb 2013 16:44:27 +0000 Error during template rendering In template C:\Users\User\Documents\Django\ParkManager\Templates\Details_Main.html, error at line 23 

early

+6
source share
2 answers

What point are you trying to name? You call the URL in the name view, but name does not exist. Since you have only one named home view, I assume that you are trying to use the view.

Neither your submission nor your URLs accept an argument, but you pass result.name as an argument to the URL.

You need to either accept the parameter in your view through def Link(request, result_name): and def Link(request, result_name): it in your URL through a regular expression with (?P<result_name>.. , or call your URL without the parameter passed :

 {% for result in results %} <a href="{% url home %}">test</a> 

Since you do not yet have logic in your representations, and you pass the parameter in a few words, rather than β€œkilling” it, I’m going to assume that you want to do the latter and simply remove the parameter from your URL call.

+6
source

This {% url name result.name%} is the problem.

Since your Link method has a keyword argument, your reverse URL pattern tag must have a corresponding keyword argument.

template.html

 <a href="{% url search result_name=result.name %}">test</a> 

Continue reading to understand what the problem is, how you configured it now, the correct way to change the URL in the template: {% url [name] [args] [kwargs]%}

Where,
[name] is one of the following: test, search_start, details, link, home or search. Or the full path to the view function, but I would recommend keeping it simple for now.
[args] may be empty or a list of arguments.
[kwargs] may be empty or a list of keyword arguments.

The documents in the url tag can be found here and describe other ways to use it ( https://docs.djangoproject.com/en/dev/ref/templates/builtins/?from=olddocs#url ).

* Aside, you will encounter problems with characters that are not allowed in URLs that are allowed in your search bar, such as spaces and ampersands.

urls.py

 url(r'^test/', Search_Page, name="test"), url(r'^search/', Search, name="search_start"), url(r'^details/', Details_Main, name="details"), url(r'^Link/(d+)/$', Link, name="link"), url(r'^$', 'Parks.views.Link', name="home"), url(r'^(?P<result_name>)/$', Link, name="search"), 

another_template.html

 <a href="{% url search result_name=result.name %}">test</a> <!-- and more examples --> <a href="{% url test %}">link to test</a> <a href="{% url search_start %}">link to search</a> <a href="{% url details %}">link to details</a> {% for a_link in links %} <a href="{% url link a_link.id %}">link to details (of a_link)</a> {% endfor %} <a href="{% url home %}">home</a> 
0
source

All Articles