Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 752
user1662290 Avatar asked Feb 04 '13 16:02

user1662290


People also ask

What is NoReverseMatch Django?

NoReverseMatch (source code) is a Django exception that is raised when a URL cannot be matched against any string or regular express in your URL configuration. A URL matching problem is often caused by missing arguments or supplying too many arguments.

How do I reference a url in Django?

Django offers a way to name urls so it's easy to reference them in view methods and templates. The most basic technique to name Django urls is to add the name attribute to url definitions in urls.py .

What is Django urls path?

The path function is contained with the django. urls module within the Django project code base. path is used for routing URLs to the appropriate view functions within a Django application using the URL dispatcher.


2 Answers

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.

like image 154
Dan Hoerst Avatar answered Sep 28 '22 01:09

Dan Hoerst


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> 
like image 24
Victor 'Chris' Cabral Avatar answered Sep 27 '22 23:09

Victor 'Chris' Cabral