Django 在捕获和转发到视图的 URL 上遇到了麻烦。 (段头字段)
我目前正在 django
上使用 slug
调用 views
,但我似乎在这方面遇到了一些麻烦。
假设我有像 de
ce
ceiling
(slug fields
)这样的数据库条目。 现在,当我调用 myapp/ce
或 myapp/de
时。它返回我想要的视图。但是当我调用 myapp/ceiling
时,它返回 404
。
未找到与查询匹配的雕塑
但它捕获了 url。
当我在 name
字段上使用大写字母时,就会出现问题。其他字段采用小写
。
我无法理解这种行为。
我的代码如下:
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)
I am currently making views
called by slug
s on django
but I seem to have some troubles on that.
Suppose I have database entries like de
ce
ceiling
(slug fields
).
Now when I call, myapp/ce
or myapp/de
. It returns the view I want. But when I call myapp/ceiling
, it returns 404
.
No sculpture found matching the query
It catches the url though.
The problem occurs when I use capital letter on the name
field. The other fields hold lowercase
.
I failed to understand this behavior.
My code is as follows:
urls.py
urlpatterns = patterns('sculptures.views',
(r'^
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)
, SculptureListView.as_view()),
(r'^(?P<slug>[\w-]+)/
views.py
, SculptureDetailView.as_view()),
)
views.py
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
查看您的代码:
在这里,您正在获取与捕获的 slug 匹配的
Sculpture
对象。然后您将获得
Sculpture
对象,其 slug 是另一个Sculpture
对象。我想知道在某些情况下这是如何工作的:)由于您有一个
DetailView
,您可以直接使用get_object()
:Looking at your code:
Here you're fetching the
Sculpture
object that matches the captured slug.And then you get the
Sculpture
object whose slug is anotherSculpture
object. I wonder how this even works in some cases :)Since you have a
DetailView
, you can directly useget_object()
: