gpt4 book ai didi

Django NoReverseMatch

转载 作者:行者123 更新时间:2023-12-02 09:44:15 26 4
gpt4 key购买 nike

我有以下设置:

/landing_pages
views.py
urls.py

urls.py中,当我尝试访问/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')

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

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

我显然做不到:

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 模式中为参数提供空格。他们会去哪里? URL 会是什么样子?它将如何运作?

如果您希望允许用户为您的 View 指定参数,则必须提供一个 URL 模式,并为这些参数留出空格。

关于Django NoReverseMatch,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6871226/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com