gpt4 book ai didi

python - Django NoReverseMatch

转载 作者:太空狗 更新时间:2023-10-29 22:21:58 24 4
gpt4 key购买 nike

我不明白这是怎么回事?我试图制作一个项目的详细信息页面,例如 http://tutorial.djangogirls.org/en/extend_your_application/index.html 上的示例它在我的项目中不起作用,但在练习中一切都很好。

错误信息: NoReverseMatch at/

Reverse for 'events.views.event_detail' with arguments '()' and keyword arguments '{u'pk': 3}' not found. 1 pattern(s) tried: ['$event/(?P<pk>[0-9]+)/$']

HTML(片段)

<div class="col-xs-6"><a class="btn btn-primary" href="{% url 'events.views.event_detail' pk=event.pk %}">Read more</a></div>
</div>

设置.py

ROOT_URLCONF = 'mysite.urls' 

应用 urls.py

from django.conf.urls import include, url
from . import views

urlpatterns = [
url(r'^$', views.events_list),
url(r'^event/(?P<pk>[0-9]+)/$', views.event_detail),
]

app views.py

from django.shortcuts import render, get_object_or_404
from django.utils import timezone
from .models import Event

def events_list(request):
events = Event.objects.filter(published_date__lte=timezone.now()).order_by('published_date')
return render(request, 'events/events_list.html', {'events': events})

def event_detail(request, pk):
event = Event.objects.get(pk=pk)
return render(request, 'events/event_detail.html', {'event': event})

最佳答案

您还没有显示您的 mysite.urls,但是从错误消息看来您做了这样的事情:

(r'^events/$', include('events.urls')),

您需要删除终止 $,因为这意味着正则表达式的结尾;在那之后没有什么可以匹配的。应该是:

(r'^events/', include('events.urls')),

请注意,您还应该提供事件 URL 名称,以便于引用:

url(r'^$', views.events_list, name='events_list'),
url(r'^event/(?P<pk>[0-9]+)/$', views.event_detail, name='event_detail'),

这样您现在就可以:

{% url 'event_detail' pk=event.pk %}

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

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