我是Django框架的新手,正在学习教程。虽然当我尝试在detail.html中包含收藏夹的输入按钮时,我得到了这个错误:
呈现模板时出错
在模板C:\用户\leo8\Desktop\Lucas\c2view03\webdeve\templates\detail.html中,第8行出错
找不到参数为'(',)'的'favorites'的反转。已尝试1个模式:
['webdev/(?P <Dreams_id>[0-9]+)/收藏夹/$']
以上是我的看法:
from .models import Dreams, Which
from django.shortcuts import render, get_object_or_404
app_name = 'webdeve'
def index(request):
all_dreams = Dreams.objects.all()
contexto = {'all_dreams': all_dreams}
return render(request, 'index.html', contexto)
def detail(request, Dreams_id):
#dream = Dreams.objects.get(pk=Dreams_id)
dream = get_object_or_404(Dreams, pk=Dreams_id)
return render(request, 'detail.html', {'dream': dream})
def favorites(request, Dreams_id):
dream = get_object_or_404(Dreams, pk=Dreams_id)
try:
selected_which = dream.which_set.get(pk=request.POST['make'])
except (KeyError, Which.DoesNotExist):
return render(request, 'detail.html', {
'dream': dream,
'error_message': "no which selected"
})
else:
selected_which.favorites = True
selected_which.save()
return render(request, 'detail.html', {'dream': dream})
和我的应用程序网址
from django.conf.urls import url
from webdeve import views
app_name = 'webdeve'
# Dreams
urlpatterns = [
url(r'^$', views.index, name='index'),
# Dreams/detail
url(r'^(?P<Dreams_id>[0-9]+)/$', views.detail, name='detail'),
# Dreams/detail/favoriteS
url(r'^(?P<Dreams_id>[0-9]+)/favorites/$', views.favorites, name='favorites'),
]
1条答案
按热度按时间xuo3flqw1#
您从未将
Dreams
从您的view
发送到您的form
,因此Dreams.id
不存在。请在模板中尝试dream.id
。