我的 URL 需要捕获以下内容:
domain/forum-name/
domain/forum-name/topic-name
我有以下 URL 定义:
url(r'^$', views.index_forums, name='index_forums'),
url(r'^(?P<forum_name>[-\w]+)/(?P<topic_name>[-\w]+)/$', views.show_topic, name='show_topic'),
url(r'^(?P<forum_name>[-\w]+)/$', views.index_topics, name='index_topics'),
当我点击域名/论坛名称之类的 URL 时,我会在 HTML 中生成 URL,如下所示:
{% url 'board:show_topic' forum_name|lower topic.title|lower %}
这给了我一个“反向...未找到”错误,我不知道为什么。我是否错误地使用了“url”?我的正则表达式有问题吗?谢谢!
编辑:添加完整的错误和模板:
NoReverseMatch at /random/
Reverse for 'show_topic' with arguments '(u'random', u'funny youtube videos')' and keyword arguments '{}' not found.
Request Method: GET
Request URL: http://localhost:8000/random/
Django Version: 1.5c2
Exception Type: NoReverseMatch
Exception Value:
Reverse for 'show_topic' with arguments '(u'random', u'funny youtube videos')' and keyword arguments '{}' not found.
Exception Location: /Users/travis/code/penv/lib/python2.7/site- packages/django/template/defaulttags.py in render, line 424
Python Executable: /Users/travis/code/penv/bin/python
Python Version: 2.7.2
Reverse for 'show_topic' with arguments '(u'random', u'funny youtube videos')' and keyword arguments '{}' not found.
1 {% extends "board/base.html" %}
2
3 {% block content %}
4 {% if topic_list %}
5 <ul>
6 {% for topic in topic_list %}
7 <li><a href="{% url 'board:show_topic' forum_name|lower topic.title|lower %}">{{topic.title}}</a></li>
8 {% endfor %}
9 </ul>
10 {% else %}
11 <p>No topics! <a href="/topic/new/">Make a new one</a>.</p>
12 {% endif %}
13 {% endblock %}