django是新手,非常喜欢简单地完成任务。但是,当我得到一个405错误声明方法时,在呈现一个通用DetailView时有问题,所以不支持。下面是我的密码。
from django.shortcuts import render, get_object_or_404, get_list_or_404
from django.views.generic import View, ListView, DetailView
from store.managers import StoreManager
from .models import Store
# Create your views here.
class StoreDetails(DetailView):
model = Store
template_name = 'store/details.html'
class StoreIndex(ListView):
model = Store
template_name = 'store/index.html'
context_object_name = 'stores'
# url
urlpatterns = [
url(r'^view/([0-9]+)/$', StoreDetails.as_view(), name='details'),
url(r'^index/$', StoreIndex.as_view(), name='index'),
]虽然我的StoreIndex视图工作得很好,但是我的StoreDetails视图出现了一个错误。尝试重写get_context_data函数,但结果相同。
发布于 2015-12-26 23:55:20
问题在于url模式。DetailView需要主键才能找到要显示的正确对象,但是模式r'^view/([0-9]+)/$'没有指定匹配的数字作为主键。试试r'^view/(?P<pk>[0-9]+)/$' (pk代表主键)。
还请参阅DetailView doocs上的示例(它提供slug而不是pk)。自定义get_context_data对于pk和slug来说不应该是必需的。
https://stackoverflow.com/questions/34475979
复制相似问题