English 中文(简体)
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?

问题回答

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 %}

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

See here

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.

You may need to be a little more specific on which view you re trying to use:

{% url appname.views.show %}

For what is is worth, I had the same issue and while I do not remember the reason why now, this resolved it for me. Example from a SCRUM app I was working on.

url(r ^$ ,  scrum.views.index ,  name= scrum-index ),




相关问题
problem serving static files to sub directories

In the development environment, static files are served properly as long as the url pattern is limited to one directory. Sub directories lose the css. For example the css processes for the template ...

Django Dynamic menu design question

I want to create dynamic menus according to user permissions. As was already discussed here and by the the documentation itself, I know that I can achieve this in the templates using the following ...

Django DRY URLs for Model access

Reader s Digest version: How do I get data (either single row if specified or full table) from a model by taking a URL argument of the model s name without hardcoding the URLconfs to match each model? ...

Pass session data onto URL

I have some information that is set in the sessions, and I was wondering if it s possible to pass this info onto the URL for the view that uses this session data. I want this to be working in such a ...

Dynamic SEO-friendly URLs

I d like to deploy dynamic URL s for my app in two ways: when viewing available vehicle, I get a link like: http://www.url.com/2006-Acura-MDX-Technology-Package I also have a filter page, so here, ...

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 ^...

django url scheme in apache and dev server

I have a a django application which is being served from apache/mod_wsgi under www.mysite.com/mysite suppose I redirect url "myapp" -> myapp/urls.py so to visit it from apache I will visit www....

热门标签