我正在 Django 中制作一个博客网站。我有一个像这样的博客模型:
class Blog(models.Model):
name = models.CharField(max_length=255)
slug = models.SlugField(max_length=255)
...
我希望每个博客的首页都位于这样的 URL:www.example.com/blog-slug/
但是,我也在使用 Flatpages,并且希望它能够匹配这样的 URL:www.example.com/flat-page/
所以像这样的 urlpatterns 不起作用:
urlpatterns = patterns('',
(r'^(?P<blog_slug>[-\w]+)/$', 'weblog_index', {}),
...
(r'^', include('django.contrib.flatpages.urls')),
)
因为所有 Flatpages URL 都会被第一个模式捕获。我想我希望第一个模式仅匹配博客模型中的有效段,但我不确定如何做到这一点。
I'm making a weblog site in Django. I have a Blog model like this:
class Blog(models.Model):
name = models.CharField(max_length=255)
slug = models.SlugField(max_length=255)
...
And I want the front pages of each blog to be at URLs like this: www.example.com/blog-slug/
However, I'm also using Flatpages and will want that to be able to match URLs like this: www.example.com/flat-page/
So urlpatterns like this won't work:
urlpatterns = patterns('',
(r'^(?P<blog_slug>[-\w]+)/
because all Flatpages URLs will get trapped by the first pattern. I guess I want the first pattern to only match valid slugs from the Blog model, but I'm not sure how to do that.
, 'weblog_index', {}),
...
(r'^', include('django.contrib.flatpages.urls')),
)
because all Flatpages URLs will get trapped by the first pattern. I guess I want the first pattern to only match valid slugs from the Blog model, but I'm not sure how to do that.
发布评论
评论(2)
你不能这样做:
既然是,Django 只会尝试与您的正则表达式匹配的第一个视图。如果该视图引发 404,它不会尝试任何其他 URL。
但是,您可以做您想做的事情,而无需向
urls.py
添加任何内容:来自 Django 文档:
基本上,只要您的博客应用程序在遇到不存在博客条目的 slug 时引发
Http404
,您就应该可以开始了。将
'django.contrib.flatpages.middleware.FlatpageFallbackMiddleware'
放置在MIDDLEWARE_CLASSES
中意味着 Django 在渲染错误页面之前,会检查FlatPage
具有与生成 404 的 URL 相匹配的路径(即,如果存在 404,则会回退到检查FlatPage
)。如果有的话,它就会渲染它。You can't do this:
Since yes, Django only tries the first view that matches your regular expression. If that view raises a 404, it doesn't try any other URLs.
However, you can do what you're trying to do without adding anything to your
urls.py
:From the Django docs:
Basically, so long as your blog apps raises an
Http404
when encountering a slug for which no blog entry exists, you should be good to go.Placing
'django.contrib.flatpages.middleware.FlatpageFallbackMiddleware'
in yourMIDDLEWARE_CLASSES
means that just before Django renders the error page, it checks for aFlatPage
with a path matching the URL that generated the 404 (i.e. if there's a 404, it falls back to checking for aFlatPage
). If there is one, it renders it.正如 Dominic 指出的那样,Flatpages 应用程序的全部要点在于它会自动匹配其他视图未捕获的任何页面。
因此,即使您无法将
weblog_index
视图限制为仅有效的 slugs,您可以在该视图中执行简单的get_object_or_404
操作,以便当没有找到匹配的 Blog slug 时,它会引发 404 错误 - 并且 404 会立即被 Flatpages 应用程序拦截,并且一切都会按照您的意愿进行。As Dominic points out, the whole point of the Flatpages app is that it automatically matches any pages that aren't caught by other views.
So, even though you can't restrict your
weblog_index
view to only valid slugs, you can do a simpleget_object_or_404
within that view, so that it raises a 404 error when no matching Blog slug is found - and that 404 is immediately intercepted by the Flatpages app, and all works as you want it to.