Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

url template tag in django template

I was trying to use the url template tag in django, but no lucky,

I defined my urls.py like this

urlpatterns = patterns('',
    url(r'^analyse/$',              views.home,  name="home"),
    url(r'^analyse/index.html',     views.index, name="index"),
    url(r'^analyse/setup.html',     views.setup, name="setup"),
    url(r'^analyse/show.html',      views.show,  name="show"),
    url(r'^analyse/generate.html',  views.generate, name="generate"),

I defined the url pattern in my view like this

{% url 'show'%}

then I got this error message

Caught an exception while rendering: Reverse for ''show'' with arguments '()' and keyword arguments '{}' not found.

Original Traceback (most recent call last): File "/Library/Python/2.5/site-packages/django/template/debug.py", line 71, in render_node result = node.render(context) File "/Library/Python/2.5/site-packages/django/template/defaulttags.py", line 155, in render nodelist.append(node.render(context)) File "/Library/Python/2.5/site-packages/django/template/defaulttags.py", line 382, in render raise e NoReverseMatch: Reverse for ''show'' with arguments '()' and keyword arguments '{}' not found.

I am wondering why django failed to render? what is the right way to define it in the tempalte?

like image 522
user192048 Avatar asked Nov 22 '09 02:11

user192048


3 Answers

IMPORTANT: this was for django 1.4. At django 1.5 it is just the opposite.

try using url names without quotes

{% url show %}

not this

{% url 'show'%}
like image 101
iskorum Avatar answered Sep 24 '22 00:09

iskorum


The problem is your single quotes around 'show'. Change this to "show" and it should work out for you.

See here

like image 42
rh0dium Avatar answered Sep 22 '22 00:09

rh0dium


You maybe have some views not implemented yet. It looks like the template engine tries to find all views from the patterns in urls.py when the {% url ... %} filter is used.

It usually shows an error for your last pattern in urls.py.

Try comment out every url pattern you did not implement yet.

Also make sure you use the full path:

{% url myapp.views.home %}

The url template filter looks really unstable. Try to keep future compatibility.

like image 40
user1283043 Avatar answered Sep 24 '22 00:09

user1283043