我的网址需要捕获以下内容:
domain/forum-name/
domain/forum-name/topic-name
我有以下网址定义:
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'),
当我点击域名/论坛名称这样的网址时,我会在HTML中生成网址,如下所示:
{% 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 %}
答案 0 :(得分:3)
尝试:
{% url 'show_topic' forum_name|lower topic_name|lower %}
编辑:
您的网址正则表达式不接受空格:
尝试:
url(r'^(?P<forum_name>[-\w]+)/(?P<topic_name>[-\w\ ]+)/$', views.show_topic, name='show_topic'),