我目前正在制作由slugs在django上调用的views,但我似乎在这方面遇到了一些麻烦。
假设我有像de ce ceiling (slug fields)这样的数据库条目。现在,当我调用myapp/ce或myapp/de时。它返回我想要的视图。但是当我调用myapp/ceiling时,它返回404。
No sculpture found matching the query
不过,它捕获的是url。
当我在name字段中使用大写字母时出现问题。其他字段保存lowercase。
我不能理解这种行为。
我的代码如下:
urls.py
urlpatterns = patterns('sculptures.views',
(r'^$', SculptureListView.as_view()),
(r'^(?P<slug>[\w-]+)/$', SculptureDetailView.as_view()),
)views.py
class SculptureDetailView(DetailView):
context_object_name = 'sculpture'
def get_queryset(self):
sculpture_slug = get_object_or_404(Sculpture, slug__iexact=self.kwargs['slug'])
return Sculpture.objects.filter(slug=sculpture_slug)发布于 2011-08-25 06:07:21
看看你的代码:
def get_queryset(self):
sculpture_slug = get_object_or_404(Sculpture, slug__iexact=self.kwargs['slug'])在这里,您将获取与捕获的插件相匹配的Sculpture对象。
return Sculpture.objects.filter(slug=sculpture_slug)然后您将获得另一个Sculpture对象为其段塞的Sculpture对象。我想知道这在某些情况下是如何工作的:)
因为您有一个DetailView,所以可以直接使用get_object()
class SculptureDetailView(DetailView):
def get_object(self):
return get_object_or_404(Sculpture, slug__iexact=self.kwargs['slug'])https://stackoverflow.com/questions/7182782
复制相似问题