需要 django 鹡鸰的指导
Need Guidance for django wagtail
目前我正在尝试将 Wagtail 与现有的 django 项目集成。
我是鹡鸰新手,还在学习鹡鸰
class BlogPage(Page):
body = RichTextField(blank=True)
categories = ParentalManyToManyField('blog.BlogCategory', blank=True)
location = models.ForeignKey('blog.Location', on_delete=models.PROTECT)
然后我将类别和位置模型注册为片段。
构建页面的最佳做法是如何包含 BlogPage 以及
特定类别/位置?
以及如何从 Django 的菜单中调用该页面
或者我在哪里可以找到将 wagtail 集成到现有 django 项目的文档
谢谢
我认为您正在寻找博客列表页面,您可以在其中列出所有博客文章,然后根据特定类别获得博客文章。
您可能想要使用 RoutablePageMixin(如果您不使用 Vue 或 React 创建 SPA)。 RoutablePageMixin 让您可以自动创建额外的子页面,而无需创建 Wagtail 子页面。
from wagtail.contrib.routable_page.models import RoutablePageMixin, route
class BlogListingPage(RoutablePageMixin, Page):
"""BlogListingPage class."""
template = 'cms/blog/blog_listing_page.html'
subpage_types = ['pages.BlogPage']
# ... other fields here
@route(r'^category/(?P<cat_slug>[-\w]*)/$', name='category_list')
def category_list(self, request, cat_slug):
"""Return posts in a certain category."""
context = self.get_context(request)
posts = BlogPage.objects.live().filter(categories__slug=cat_slug).order_by('-pub_date')
context['posts'] = posts
return render(request, 'cms/blog/blog_category_page.html', context)
注意我未测试此代码,您可能需要修复任何错误并根据您的需要进行调整。
上面的代码将获取您的博客列表页面(比如说它的 localhost:8000/blog/)并创建一个类别列表页面(即 localhost:8000/blog/category/topic-slug/)
topic-slug
将被传递到 category_list()
方法,然后您可以在其中根据其所在的类别过滤您的 BlogPage。它会将 posts
添加到您的页面,并呈现不同的列表页面,您可以在其中自定义模板。
我检查已经有一段时间了,但是 Wagtail Bakery Demo 可能有这方面的例子(还有很多非常棒的东西)。
您也可以在 https://docs.wagtail.io/en/latest/reference/contrib/routablepage.html 阅读有关 Wagtail 可路由页面的更多信息。
目前我正在尝试将 Wagtail 与现有的 django 项目集成。
我是鹡鸰新手,还在学习鹡鸰
class BlogPage(Page):
body = RichTextField(blank=True)
categories = ParentalManyToManyField('blog.BlogCategory', blank=True)
location = models.ForeignKey('blog.Location', on_delete=models.PROTECT)
然后我将类别和位置模型注册为片段。
构建页面的最佳做法是如何包含 BlogPage 以及 特定类别/位置?
以及如何从 Django 的菜单中调用该页面
或者我在哪里可以找到将 wagtail 集成到现有 django 项目的文档
谢谢
我认为您正在寻找博客列表页面,您可以在其中列出所有博客文章,然后根据特定类别获得博客文章。
您可能想要使用 RoutablePageMixin(如果您不使用 Vue 或 React 创建 SPA)。 RoutablePageMixin 让您可以自动创建额外的子页面,而无需创建 Wagtail 子页面。
from wagtail.contrib.routable_page.models import RoutablePageMixin, route
class BlogListingPage(RoutablePageMixin, Page):
"""BlogListingPage class."""
template = 'cms/blog/blog_listing_page.html'
subpage_types = ['pages.BlogPage']
# ... other fields here
@route(r'^category/(?P<cat_slug>[-\w]*)/$', name='category_list')
def category_list(self, request, cat_slug):
"""Return posts in a certain category."""
context = self.get_context(request)
posts = BlogPage.objects.live().filter(categories__slug=cat_slug).order_by('-pub_date')
context['posts'] = posts
return render(request, 'cms/blog/blog_category_page.html', context)
注意我未测试此代码,您可能需要修复任何错误并根据您的需要进行调整。
上面的代码将获取您的博客列表页面(比如说它的 localhost:8000/blog/)并创建一个类别列表页面(即 localhost:8000/blog/category/topic-slug/)
topic-slug
将被传递到 category_list()
方法,然后您可以在其中根据其所在的类别过滤您的 BlogPage。它会将 posts
添加到您的页面,并呈现不同的列表页面,您可以在其中自定义模板。
我检查已经有一段时间了,但是 Wagtail Bakery Demo 可能有这方面的例子(还有很多非常棒的东西)。
您也可以在 https://docs.wagtail.io/en/latest/reference/contrib/routablepage.html 阅读有关 Wagtail 可路由页面的更多信息。