4

I am getting an error creating a link in my Django template.

My template looks like this:

<a href="{% url 'location_detail' pk=location.id %}">{{ location.name }}</a>

My urls.py looks like:

url(r'^location(?P<pk>\d+)/$', views.location_detail, name="location_detail"),

My view looks like:

def location_detail(request, pk=None):

I get the error:

Reverse for views.location_detail with arguments '()' and keyword arguments '{u'pk': 1L}' not found.

I'm using Django 1.5 and python 2.7.2

Thanks!

4

2 回答 2

10

问题是我在主项目 urls.py 上有一个命名空间:

url(r'^com/', include('com.urls', namespace="com")),

将网址更改为:

{% url 'com:location_detail' pk=location.id %}

那成功了

于 2013-07-23T21:18:59.687 回答
3

您已经为您的 url 模式命名,因此您应该在{% url %}调用中使用该名称:

{% url 'location_detail' pk=location.id %}
于 2013-07-23T18:47:11.503 回答