NoReverseMatch Django URL
Asked Answered
N

3

10

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

Nevermore answered 4/2, 2013 at 16:49 Comment(0)
F
9

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.

Fruiterer answered 4/2, 2013 at 16:55 Comment(3)
when I remove the later part the url goes straight to 127.0.0.1/8000 (without out anything ending) Updated the post to match my revised code...still noluckNevermore
You are passing the URL to the name view. The URL for this view is 127.0.0.1/8000, so it is doing just as you are asking it to. The new URL you've added is incorrect. It needs a regex after (?P<result_name>) to catch the variable that you are passing, but you are passing a multi-word variable and this needs to be combined - that is called a slug. Have you checked out the Django tutorial? I highly suggest you check that out as a first step. docs.djangoproject.com/en/1.4/intro/tutorial01Fruiterer
I had read through the documentation regaridng URL tags but it didn't help.However, after looking at your answer again, it worked for me code now reads: <a href="/results/{{ result.id }} ">test</a> Thanks :DNevermore
D
1

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> 
Densify answered 4/2, 2013 at 18:43 Comment(2)
Thanks for your detailed response. Unfortunatley it didn't work. However, I read through the online documentation and found a way to get my desired result without the url tag. My template now reads: <a href="/results/{{result.id}}">test</a> and that seems to work fineNevermore
It is not proper form to create urls in this way (Violated DRY principle in a subtle way). I would suggest getting the url reverser to work to appease the django gods. What is the error when using {% url link a_link.id %}?Analyzer
D
0

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

Discoloration answered 7/6, 2023 at 9:17 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.