wigtail的分组页面

时间:2018-01-16 17:58:04

标签: wagtail

如何在wagtail cms中对页面进行分组?

我的项目中有城市的页面模型。无障碍投掷/城市名/:

  • www.example.com/london /
  • www.example.com/berlin /
  • www.example.com/newyork /
  • ...

在我的数据库中拥有城市的阴影之后,我在wagtail cms的根视图变得不清楚了。我想对所有城市进行分组,我该怎么做?

通常你会创建一个父对象,但在这种情况下我的父对象是根。我可以以某种方式创建虚拟父页面吗?

2 个答案:

答案 0 :(得分:0)

您无法在管理员的资源管理器部分中拥有虚拟父页面,您只能(当前)在其本机树结构中导航/管理页面。

但是,您可能希望使用modeladmin来提供专门用于编辑城市页面的管理员的单独部分。使用modelAdmin还可以让您从资源管理器部分隐藏城市页面类型,并为您提供大量有关如何列出,搜索和过滤页面的自定义。

以下是文档中稍加修改的示例。

# file: myapp/wagtail_hooks.py
from wagtail.contrib.modeladmin.options import (ModelAdmin, modeladmin_register)
from .models import CityPage


class CityPageModelAdmin(ModelAdmin):
    model = CityPage
    menu_label = 'Cities'  # ditch this to use verbose_name_plural from model
    menu_icon = 'grip'  # change as required
    menu_order = 200  # will put in 3rd place (000 being 1st, 100 2nd)
    add_to_settings_menu = False  # or True to add your model to the Settings sub-menu
    exclude_from_explorer = True # setting to true will exclude pages of this type from Wagtail's explorer view
    list_display = ('title', 'country', 'other_example_field', 'live')
    list_filter = ('live', 'country')
    search_fields = ('title',) # remember trailing comma on single item sets

# Now you just need to register your customised ModelAdmin class with Wagtail
modeladmin_register(CityPageModelAdmin)

答案 1 :(得分:0)

在Wagtail中没有虚拟父页面这样的东西,我不确定你是否希望所有城市页面都在拥有多个父级的单个父级下(例如按国家/地区分组城市)。我会假设后者,但请告诉我,如果情况并非如此,我会更新答案。

所以你有几个选择:

1)创建一个CountryPage作为Homepage的孩子,并在其下生活CityPage。然后您接受该网址为/france/paris/

2)创建一个CountryPage作为Homepage的孩子,并在其下生活所有CityPage(与选项1相同),但您也可以Homepage一个RoutablePage,它将提供城市页面的内容。

from django.shortcuts import get_object_or_404
from wagtail.wagtailcore.models import Page
from wagtail.contrib.wagtailroutablepage.models import RoutablePageMixin, route


class Homepage(RoutablePageMixin, Page):
    @route(r'^(\w+)/$', name='city')
    def city(self, request, city_slug):
        city = get_object_or_404(CityPage, slug=city_slug)
        return city.serve(request)

然而,这有一些警告: - CityPage仍可在/france/paris/处使用,因此您需要设置规范网址,以确保没有双重索引。 - CountryPage仍然可以/france/使用,您可能不需要。 - 名字可能会发生冲突。例如,如果您的CountryPage标题为luxembourg,其中CityPage具有与子项相同的标记。当您访问/luxembourg/时,您会希望Homepage选择它并服务器/luxembourg/luxembourg/,但它不会,因为CountryPage会在/luxembourg/处获取该网址。已经Homepage

3)如果你的城市不一定需要是一个页面(它们比其他任何东西都更多,并且没有孩子),你可以将它们转换为模型。此模型将在管理界面中显示为snippetsmodeladmin,并由from django.shortcuts import get_object_or_404, render from wagtail.wagtailcore.models import Page from wagtail.contrib.wagtailroutablepage.models import RoutablePageMixin, route class Homepage(RoutablePageMixin, Page): @route(r'^(\w+)/$', name='city') def city(self, request, city_slug): city = get_object_or_404(CityModel, slug=city_slug) context = self.get_context(request, *args, **kwargs) context['city'] = city return render(request, 'city.html', context) 提供(与选项2类似)。

{{1}}