Django:没有反向网址与多个参数匹配 [英] Django: no reverse url match with multiple parameters

查看:54
本文介绍了Django:没有反向网址与多个参数匹配的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的URL需要捕获以下内容:

My URLs need to capture the following:

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'),

当我点击网址(例如域名/论坛名称)时,我会在HTML中生成如下网址:

When I hit a URL like domain/name-of-forum, I generate URLs in the HTML like so:

{% url 'board:show_topic' forum_name|lower topic.title|lower %}

这给了我反向...未找到错误,我无法弄清原因。我是否错误地使用 url?我的正则表达式有问题吗?谢谢!

This gives me a "Reverse...not found" error, and I cannot figure out why. Am I using "url" incorrectly? Is there a problem with my regular expression? Thanks!

编辑:添加完整的错误和模板:

adding full error and template:

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 %}


推荐答案

尝试:

{% url 'show_topic' forum_name|lower topic_name|lower %}






编辑:


您的网址正则表达式不接受空格:

Your url regex doesn't accept spaces:

尝试:

url(r'^(?P<forum_name>[-\w]+)/(?P<topic_name>[-\w\ ]+)/$', views.show_topic, name='show_topic'),

这篇关于Django:没有反向网址与多个参数匹配的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆