Django:带有公共前缀的URL和带有param的{%url%}

时间:2015-06-15 10:55:06

标签: python-3.x django-views django-urls django-1.8

在我的urls.py中,我定义了这样的内容:

url(r'file-(?P<fid>\d+)\.html', detail, name='detail'),
url(r'file-(?P<fid>\d+)/history', history, name='history'),
url(r'file-(?P<fid>\d+)/edit', edit, name='edit'),
url(r'file-(?P<fid>\d+)/comments', comments, name='comments'),
url(r'file-(?P<fid>\d+)/delete', delete, name='delete'),

并在视图中:

<a href="{% url 'filer:detail' file.id %}">{{ file.name }}</a>&nbsp;
<a href="{% url 'filer:delete' file.id %}"><img src="{% static 'filer/images/delete.png' %}" alt="delete" /></a>

根据docs我可以这样做:

url(r'^file-(?P<fid>\d+)', include(
    url(r'^\.html', detail, name='detail'),
    url(r'^/history', history, name='history'),
    url(r'^/edit', edit, name='edit'),
    url(r'^/comments', comments, name='comments'),
    url(r'^/delete', delete, name='delete'),
)),

但如果我更改为此包含模式,那么我会收到此错误:

NoReverseMatch at /files/

Reverse for 'detail' with arguments '(2,)' and keyword arguments '{}' not found. 0 pattern(s) tried: []

Request Method: GET
Request URL:    xxx.xxx.xxx.xxx
Django Version: 1.8.2
Exception Type: NoReverseMatch
Exception Value: Reverse for 'detail' with arguments '(2,)' and keyword arguments '{}' not found. 0 pattern(s) tried: []

我知道为什么会收到此错误 - url(r'^\.html', detail, name='detail'),中没有参数 - 我的问题是:如何使用{% url %}在视图中使用此网址模式?

Django:1.8.2 Python:3.4.2

0 个答案:

没有答案