Reputation: 105
New to django and really like the simplicity in getting things done. However having problem rendering a generic DetailView as I get a 405 error stating method not supported. Below is my code.
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'),
]
While my StoreIndex view works perfectly, I get an error for my StoreDetails view. Tried overriding get_context_data function but same result.
Upvotes: 1
Views: 1608
Reputation: 490
The problem is in the url pattern. The DetailView
needs the primary key to find the right object to display, but the pattern r'^view/([0-9]+)/$'
does not specify that the matching number should be used as the primary key. Try r'^view/(?P<pk>[0-9]+)/$'
(pk
stands for primary key).
Also see the example at DetailView doocs (which provides slug
instead of pk
). Custom get_context_data
should not be neede for pk
and slug
.
Upvotes: 2