如何使 Django urlpattern 仅接受模型中的 slugs

发布于 2024-09-19 20:55:31 字数 611 浏览 5 评论 0 原文

我正在 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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

未蓝澄海的烟 2024-09-26 20:55:31

你不能这样做:

我想我希望第一个模式仅匹配博客模型中的有效 slugs,但我不确定如何做到这一点。

既然是,Django 只会尝试与您的正则表达式匹配的第一个视图。如果该视图引发 404,它不会尝试任何其他 URL。

但是,您可以做您想做的事情,而无需向 urls.py 添加任何内容:

来自 Django 文档

要安装 Flatpages 应用程序,请按照以下步骤操作:

  1. 通过将 'django.contrib.sites' 添加到您的 INSTALLED_APPS 设置(如果尚未存在)来安装站点框架。

  2. 另请确保您已将 SITE_ID 正确设置为设置文件所代表的网站的 ID。通常为 1(即 SITE_ID = 1),但如果您使用站点框架来管理多个站点,则它可能是不同站点的 ID。

  3. 'django.contrib.flatpages' 添加到您的 INSTALLED_APPS 设置中。

  4. 'django.contrib.flatpages.middleware.FlatpageFallbackMiddleware' 添加到您的 MIDDLEWARE_CLASSES 设置中。

  5. 运行命令manage.pysyncdb

基本上,只要您的博客应用程序在遇到不存在博客条目的 slug 时引发 Http404,您就应该可以开始了。

'django.contrib.flatpages.middleware.FlatpageFallbackMiddleware' 放置在 MIDDLEWARE_CLASSES 中意味着 Django 在渲染错误页面之前,会检查 FlatPage 具有与生成 404 的 URL 相匹配的路径(即,如果存在 404,则会回退到检查 FlatPage)。如果有的话,它就会渲染它。

You can't do this:

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.

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:

To install the flatpages app, follow these steps:

  1. Install the sites framework by adding 'django.contrib.sites' to your INSTALLED_APPS setting, if it’s not already in there.

  2. Also make sure you’ve correctly set SITE_ID to the ID of the site the settings file represents. This will usually be 1 (i.e. SITE_ID = 1, but if you’re using the sites framework to manage multiple sites, it could be the ID of a different site.

  3. Add 'django.contrib.flatpages' to your INSTALLED_APPS setting.

  4. Add 'django.contrib.flatpages.middleware.FlatpageFallbackMiddleware' to your MIDDLEWARE_CLASSES setting.

  5. Run the command manage.py syncdb.

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 your MIDDLEWARE_CLASSES means that just before Django renders the error page, it checks for a FlatPage with a path matching the URL that generated the 404 (i.e. if there's a 404, it falls back to checking for a FlatPage). If there is one, it renders it.

败给现实 2024-09-26 20:55:31

正如 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 simple get_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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文