Home >  > Wagtail教程(二)

Wagtail教程(二)

一、page类
位于wagtail.core.models.py下面。

class Page(AbstractPage, index.Indexed, ClusterableModel, metaclass=PageBase):
    title = models.CharField(
        verbose_name=_('title'),
        max_length=255,
        help_text=_("The page title as you'd like it to be seen by the public")
    )
    # to reflect title of a current draft in the admin UI
    draft_title = models.CharField(
        max_length=255,
        editable=False
    )
    slug = models.SlugField(
        verbose_name=_('slug'),
        allow_unicode=True,
        max_length=255,
        help_text=_("The name of the page as it will appear in URLs e.g http://domain.com/blog/[my-slug]/")
    )
    content_type = models.ForeignKey(
        ContentType,
        verbose_name=_('content type'),
        related_name='pages',
        on_delete=models.SET(get_default_page_content_type)
    )
    live = models.BooleanField(verbose_name=_('live'), default=True, editable=False)
    has_unpublished_changes = models.BooleanField(
        verbose_name=_('has unpublished changes'),
        default=False,
        editable=False
    )
    url_path = models.TextField(verbose_name=_('URL path'), blank=True, editable=False)
    owner = models.ForeignKey(
        settings.AUTH_USER_MODEL,
        verbose_name=_('owner'),
        null=True,
        blank=True,
        editable=True,
        on_delete=models.SET_NULL,
        related_name='owned_pages'
    )

    seo_title = models.CharField(
        verbose_name=_("page title"),
        max_length=255,
        blank=True,
        help_text=_("Optional. 'Search Engine Friendly' title. This will appear at the top of the browser window.")
    )

    show_in_menus_default = False
    show_in_menus = models.BooleanField(
        verbose_name=_('show in menus'),
        default=False,
        help_text=_("Whether a link to this page will appear in automatically generated menus")
    )
    search_description = models.TextField(verbose_name=_('search description'), blank=True)

    go_live_at = models.DateTimeField(
        verbose_name=_("go live date/time"),
        blank=True,
        null=True
    )
    expire_at = models.DateTimeField(
        verbose_name=_("expiry date/time"),
        blank=True,
        null=True
    )
    expired = models.BooleanField(verbose_name=_('expired'), default=False, editable=False)

    locked = models.BooleanField(verbose_name=_('locked'), default=False, editable=False)
    locked_at = models.DateTimeField(verbose_name=_('locked at'), null=True, editable=False)
    locked_by = models.ForeignKey(
        settings.AUTH_USER_MODEL,
        verbose_name=_('locked by'),
        null=True,
        blank=True,
        editable=False,
        on_delete=models.SET_NULL,
        related_name='locked_pages'
    )

二、通过python添加页面
(一)在django的项目中新建一个DB_tools的文件夹,再在文件夹中建立一个import_da.py的文件,首先加入以下代码。

import sys
import os

script_dir = os.path.split(os.path.realpath(__file__))[0]                   # 获取脚本所在的目录
prj_dir = os.path.dirname(script_dir)                                       # 获取其上级目录,及django项目所在的目录。
sys.path.append(prj_dir)                                                    # 在python系统路径中加入上一步得到的目录。
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "mysite.settings.dev")      # 设置DJANGO_SETTINGS_MODULE变量。cpsite即settings.py所在的文件夹名称


import django
django.setup()


from wagtail.core.models import Page
from blog.models import BlogPage


parent_page =  Page.objects.get(title='index02').specific

news_page = BlogPage(
    title = 'title test 07',
    slug = 'title_test_07',
    body='Something pleasant to hear.',
    intro="desc",
)

parent_page.add_child(instance=news_page)
#news_page.save()
news_page.save_revision().publish() 

(二)经测试可用,可是无法添加tags,即使在代码中加上tags,发布后也没有tags。
(三)slug不能相同,文章的标题可以相同。

三、tags
(一)看看它的数据表。

(二)添加tags
如果要实现添加tags,只需要在数据库中插入“content_object_id”和"tag_id"即可。

暧昧帖

本文暂无标签