Django url templatetag (but not reverse() ) error: Caught NoReverseMatch while rendering

心已入冬 提交于 2020-01-02 01:20:12

问题


I'm trying to use the url template tag as such:

{% url all-labs-map %}

but when i view the page, i get this error:

Caught NoReverseMatch while rendering: Reverse for 'all-labs-map' with arguments '()' and keyword arguments '{}' not found.

When I use the template tag like this:

{% url gmaps.views.all_labs %}

It works just fine.

Here's the URL conf:

urlpatterns = patterns('gmaps.views',
    url(r'^lab_list/$', 'all_labs', name="all-labs-map" ),
)

I tried using the django shell to see if there was a problem with the named URL, but using

reverse('all-labs-map') 

returns the correct URL.

Any ideas on what's going on?

Thanks!

Majd

EDIT:

I am using django 1.2 on ubuntu with nginx server and gunicorn and virtualenv. I'm having another trouble with a custom tag where the library loads, but the tag itself is not recognized even though i'm using the correct tag registration syntax. Any ideas would be very greatly appreciated!


回答1:


This is still high in Google results, but no one has answered it correctly yet. The key is this:

{% load url from future %}

Prior to Django 1.3, this was the syntax for the url templatetag:

{% url view_name arg1 %}

In Django 1.5, this will be the syntax:

{% url "view_name" arg1 %}

Starting in Django 1.3, the old version works but gives you a deprecation warning, telling you to {% load url from future %} and switch to the new version of that templatetag, in preparation for Django 1.5




回答2:


Have you tried enclosing the name of the url in quotes like so:

{% url "all-labs-map" %}

or

{% url 'all-labs-map' %}

I've had some problems with URLs once and this seemed to help. Also regarding @user608133 comment - you need to restart gunicorn rather than nginx, as nginx is just a proxy...




回答3:


This error might occur if you have another url with the same name overriding this one that requires multiple parameters. Is there any duplicates found when you do a search in your entire project for 'all-labs-map'?



来源:https://stackoverflow.com/questions/5157976/django-url-templatetag-but-not-reverse-error-caught-noreversematch-while-r

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!