假设我为我的网站创建了一个博客。 Wagtail 管理中的树结构设置如下所示:
主页 > 博客索引 > 博客文章
是否可以将博客索引页面保留在管理页面树中,但将其从 URL 中删除,以便我的 URL 看起来像这样:
主页 > 博客文章
我正在为博客索引页面分配一个自定义组,允许他们仅编辑他们创建的博客帖子,这就是为什么博客索引需要保留在管理端的树中。
我对 routablepagemixin 做了一些工作,但没有从 URL 中删除任何内容,只是添加了它。
最佳答案
我不完全确定 RoutablePageMixin
是否是解决此问题的正确方法,但这就是我之前解决此问题的方法。
这是一个示例,说明如何使用 RoutablePageMixin
和 route
(注意:我很快就把它切碎了,没有测试,你可能需要做一些调整)
from django.http import HttpResponseRedirect
from wagtail.contrib.routable_page.models import RoutablePageMixin, route
from wagtail.core.models import Page
from blog.models import BlogPage
class HomePage(RoutablePageMixin, Page):
"""A home page class."""
# HomePage Fields here...
# This route will collect the blog slug
# We'll look for the live BlogPost page.
@route(r"^(?P<blog_slug>[-\w]*)/$", name="blog_post")
def blog_post(self, request, blog_slug, *args, **kwargs):
try:
# Get the blog page
blog_page = BlogPage.objects.live().get(slug=blog_slug)
except BlogPage.DoesNotExist:
# 404 or post is not live yet
return HttpResponseRedirect("/")
except Exception:
# Handle your other exceptions here; here's a simple redirect back to home
return HttpResponseRedirect("/")
# Additional logic if you need to perform something before serving the blog post
# Let the blog post page handle the serve
return blog_page.specific.serve(request, *args, **kwargs)
另一件需要注意的事情:您需要更改原始博客文章页面上的站点地图 url,这样它们就不会在 中显示为
。 /blog/blog-slug/
>站点地图.xml
关于django - 有没有办法隐藏从 Wagtail 管理页面树创建的 URL 的一部分?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56087615/