Django NoReverseMatch [英] Django NoReverseMatch

查看:112
本文介绍了Django NoReverseMatch的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下设置:

/landing_pages
    views.py
urls.py

urls.py 我有以下在django.conf中尝试访问 / competition

In urls.py I have the following which works when I try to access /competition:

from django.conf.urls.defaults import *
from django.conf import settings
from django.views.generic.simple import direct_to_template

from django.contrib import admin
admin.autodiscover()

urlpatterns = patterns('',
    url(r'^competition$', 'landing_pages.views.page', {'page_name': 'competition'}, name="competition_landing"),
)

我的 views.py 有这样的东西:

def page(request, page_name):
    return HttpResponse('ok')

然后在模板中我试图这样做:

Then in a template I'm trying to do this:

{% load url from future %}
<a href="{% url 'landing_pages.views.page' page_name='competition'%}">
    Competition
</a>

我显然不能做:

在渲染时捕获NoReverseMatch:反向'landing_pages.views.page'与参数'()'和关键字参数'{'page_name':u'competition'}未找到。

Caught NoReverseMatch while rendering: Reverse for 'landing_pages.views.page' with arguments '()' and keyword arguments '{'page_name': u'competition'}' not found.

我做错了什么?

推荐答案

你在你的评论中询问DrTyrsa为什么你不能使用args或kwargs。只要想一会儿 {%url%} 标签输出 - 顾名思义,用户可以点击的实际URL。但是您在参数的URL模式中没有提供空格。他们会去哪里?网址是什么样的?它如何工作?

You ask in your comment to DrTyrsa why you can't use args or kwargs. Just think about it for a moment. The {% url %} tag outputs - as the name implies - an actual URL which the user can click on. But you've provided no space in the URL pattern for the arguments. Where would they go? What would the URL look like? How would it work?

如果要允许用户为视图指定参数,则必须提供一个带有空格的URL模式。

If you want to allow the user to specify arguments to your view, you have to provide a URL pattern with a space for those arguments to go.

这篇关于Django NoReverseMatch的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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