user1662290
user1662290

Reputation: 474

NoReverseMatch Django URL

Currently suffering with a NoReverseMatch error with Django URL tag. Been following The definitive guide to Django, the Django doucmentation and searched around here and 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

thanks in advance

Upvotes: 10

Views: 36525

Answers (3)

Amina K M
Amina K M

Reputation: 63

Check whether that field exists in model. If field exists, check for spelling whether it is same as in model

Upvotes: 0

Dan Hoerst
Dan Hoerst

Reputation: 6320

What view are you attempting to call? You are calling the URL on the name view, but the name does not exist. Since you only have one named view, home, I'll assume that's the view you are trying to use.

Neither your view nor your URLs takes an argument, yet you are passing result.name as an argument in the URL.

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

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

Since you have no logic in your views yet, and are passing a multi-word parameter and not "slugifying" it - I'm going to assume you want to do the latter and just remove the parameter from your URL call.

Upvotes: 9

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

Since your Link method has a keyword argument, your url reverse template tag should have a matching keyword argument.

template.html

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

Continued reading to make it clear what the problem is, as you have set it up now, the proper way to reverse a url in a template would be this:
{% url [name] [args] [kwargs] %}

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

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

*As an aside, you are going to run into problems with characters that are not allowed in urls that are allowed in your search string like 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> 

Upvotes: 1

Related Questions