带有额外可排序和摘要的问题保存页面

问题描述 投票:1回答:1

在保存w中的文章页面时遇到一些问题,我有3个类:ArticlePageModule,ArticlePageModulePlacement和ArticlePage

错误说:

File "/opt/miniconda3/envs/nefodev/lib/python3.7/site-packages/django/db/models/base.py", line 1222, in full_clean
    raise ValidationError(errors)
django.core.exceptions.ValidationError: {'title': ['This field cannot be blank.']}

我的意图是要有一部分动态文章,您可以使用以下代码段添加:

  • 带有几篇文章的页面(仅预览)。
  • 通过单击文章发送到您自己的文章页面
  • 在w管理员中,您可以按发布日期管理文章

我的代码(此代码支持:this

from django.db import models
from wagtail.core.fields import StreamField
from wagtail.admin.edit_handlers import FieldPanel, StreamFieldPanel, InlinePanel
from wagtail.images.edit_handlers import ImageChooserPanel
from wagtail.core.models import Page, Orderable
from modelcluster.fields import ParentalKey
from wagtail.snippets.models import register_snippet
from wagtail.snippets.edit_handlers import SnippetChooserPanel
from wagtail.images.blocks import ImageChooserBlock
from wagtail.core import blocks
from wagtail.contrib.routable_page.models import RoutablePageMixin, route


# Create your models here.
@register_snippet
class ArticlePageModule(models.Model):

    title = models.CharField(max_length=80)
    subtitle = models.CharField(max_length=50)
    thumbImage =  models.ForeignKey(
        'wagtailimages.Image',
        null=True,
        blank=True,
        on_delete=models.SET_NULL,
        related_name='+'
    )

    panels = [
        FieldPanel('title'),
        FieldPanel('subtitle'),
        ImageChooserPanel('thumbImage')
    ]

    def __str__(self):
        return self.title

class ArticlePageModulePlacement(Orderable, models.Model):
    page = ParentalKey('blog.ArticlePage', on_delete=models.CASCADE, related_name='article_module_placements')

    article_module = models.ForeignKey(ArticlePageModule, on_delete=models.CASCADE, related_name='+')

    slug = models.SlugField()

    panels = [
        FieldPanel('slug'),
        SnippetChooserPanel('article_module'),
    ]

class ArticlePage(Page, RoutablePageMixin):

    content = StreamField([
        ('heading', blocks.CharBlock()),
        ('content', blocks.RichTextBlock()),
        ('image', ImageChooserBlock()),
    ])

    content_panels = [
        InlinePanel('article_module_placements', label="Modules"),
        StreamFieldPanel('content')
    ]

    @route(r'^module/(?P<slug>[\w\-]+)/$')
    def page_with_module(self, request, slug=None):
        self.article_module_slug = slug
        return self.serve(request)


    def get_context(self, request):
        context = super().get_context(request)

        if hasattr(self, 'article_module_slug'):
            context['ArticlePageModule'] = self.article_module_placements.filter(slug = self.article_module).first().article_module

        return context
python django wagtail
1个回答
0
投票
您从content_panels中省略了页面标题字段-如错误消息所述,这是页面模型上的必填字段。通常,您可以从Page.content_panels中将其插入:

class ArticlePage(Page, RoutablePageMixin): # ... content_panels = Page.content_panels + [ InlinePanel('article_module_placements', label="Modules"), StreamFieldPanel('content') ]

© www.soinside.com 2019 - 2024. All rights reserved.