从Page.promote_panel中删除slug后出错[Django Wagtail]

eimct9ow  于 2023-11-20  发布在  Go
关注(0)|答案(1)|浏览(91)

Page.promote_panels中删除“slug”字段后,我得到以下错误。
我用的是wagtail 5.1。

KeyError at /admin/pages/add/blog/blogdetailpage/10/
"Key 'slug' not found in 'BlogDetailPageForm'. Choices are: category, classes, comment_notifications, container_size, containerized, content, expire_at, go_live_at, publish_at, search_description, seo_title, site, title."

字符串
我正在用下面的代码自动生成slug,所以我认为没有必要保留它。

class BlogDetailPage(Page):
    """Parental Blog Detail Page."""
    template = "blog/blog_detail_page.html"

    site = models.ForeignKey(
        "wagtailcore.Site",
        null=True,
        blank=True,
        on_delete=models.SET_NULL,
        related_name="+",
        help_text="Select a site where this will be visible. \nLeave blank to make page visible on all sites.",
    )
    container_size = models.CharField(
        choices=container_sizes,
        null=True,
        blank=True,
        max_length=255,
    )
    classes = models.CharField(max_length=255, null=True, blank=True)

    objects = BlogDetailManager()

    content = fields.StreamField([
        ("section", Section()),
        ("spacer", Spacer()),
        ("filler", Filler()),
        ("banner", Banner()),
        ("column_two", ColumnTwo()),
        ("heading", Heading()),
        ("rich_text", SafeTextBlock()),
    ], use_json_field=True, blank=True)

    content_panels = Page.content_panels + [
        MultiFieldPanel([
            FieldPanel("classes"),
            FieldPanel("containerized"),
            FieldPanel("container_size"),
        ], heading="Container"),
        FieldPanel("content"),
    ]

    category = models.ForeignKey(
        "blog.BlogCategory",
        null=True,
        blank=True,
        on_delete=models.SET_NULL,
        related_name="+",
    )

    created_at = models.DateTimeField(auto_now_add=True)
    updated_at = models.DateTimeField(auto_now=True)
    publish_at = models.DateTimeField(auto_now=False, auto_now_add=False, null=True, blank=True)

    promote_panels = [
        FieldPanel("site"),
        FieldPanel("publish_at"),
        FieldPanel("category"),
    ]

    settings_panels = Page.settings_panels + [
        FieldPanel("seo_title"),
        FieldPanel("search_description"),
    ]

    parent_page_types = [
        'blog.BlogListingPage',
    ]
    subpage_types = []

    class Meta:
        verbose_name = "Blog Detail Page"
        verbose_name_plural = "Blog Detail Pages"
        ordering = ["-publish_at"]

    def save(self, request=None, *args, **kwargs):
        if request and not (
                # cast to true boolean
                not not self.site or 
                not not self.site_id):

            self.site = Site.find_for_request(request)

        slug = slugify(f"{self.title}-{self.created_at}")
        if not self.slug or self.slug != slug:
            self.slug = slug
        if not self.publish_at:
            self.publish_at = datetime.now()
        super().save(*args, **kwargs)


有没有人熟悉做一些沿着这条线?

编辑

模型管理员:

from wagtail.contrib.modeladmin.options import ModelAdmin, modeladmin_register
from wagtail.models import Site
from .models import BlogDetailPage
from django.db.models import Q

class BlogDetailPageAdmin(ModelAdmin):
    model = BlogDetailPage

    add_to_settings_menu = False 

    def get_queryset(self, request):
        qs = super().get_queryset(request)
        if request.user.is_superuser:
            return qs
        
        filterQuery = Q(site=Site.find_for_request(request)) | Q(site__isnull=True)
        if not request.user.is_superuser:
            filterQuery = filterQuery | Q(owner=request.user)

        return qs.filter(filterQuery)
    
modeladmin_register(BlogDetailPageAdmin)


traceback:

Template error:
In template C:\Users\Nigel\site\env\Lib\site-packages\wagtail\admin\templates\wagtailadmin\panels\object_list.html, error at line 9
   Key 'slug' not found in 'BlogDetailPageForm'. Choices are: category, classes, comment_notifications, container_size, containerized, content, expire_at, go_live_at, publish_at, search_description, seo_title, site, title.
   1 : {% load wagtailadmin_tags %}
   2 : 
   3 : <div class="w-form-width" {% include "wagtailadmin/shared/attrs.html" with attrs=self.attrs %}>
   4 :     {% if self.help_text %}
   5 :         {% help_block status="info" %}{{ self.help_text }}{% endhelp_block %}
   6 :     {% endif %}
   7 :     {% for child, identifier in self.visible_children_with_identifiers %}
   8 :         {% panel id_prefix=self.prefix id=identifier classname=child.classes|join:' ' attrs=child.attrs heading=child.heading heading_size="label" icon=child.icon id_for_label=child.id_for_label is_required=child.is_required %}
   9 :              {% component child %} 
   10 :         {% endpanel %}
   11 :     {% endfor %}
   12 : </div>
   13 : 

Traceback (most recent call last):
  File "C:\Users\Nigel\site\env\Lib\site-packages\django\forms\forms.py", line 178, in __getitem__
    field = self.fields[name]
            ^^^^^^^^^^^^^^^^^

During handling of the above exception ('slug'), another exception occurred:
  File "C:\Users\Nigel\site\env\Lib\site-packages\django\core\handlers\exception.py", line 55, in inner
    response = get_response(request)
               ^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\Nigel\site\env\Lib\site-packages\django\core\handlers\base.py", line 220, in _get_response
    response = response.render()
               ^^^^^^^^^^^^^^^^^
  File "C:\Users\Nigel\site\env\Lib\site-packages\wagtail\admin\auth.py", line 184, in overridden_render
    return render()
           ^^^^^^^^
  File "C:\Users\Nigel\site\env\Lib\site-packages\django\template\response.py", line 114, in render
    self.content = self.rendered_content
                   ^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\Nigel\site\env\Lib\site-packages\django\template\response.py", line 92, in rendered_content
    return template.render(context, self._request)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\Nigel\site\env\Lib\site-packages\django\template\backends\django.py", line 61, in render
    return self.template.render(context)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\Nigel\site\env\Lib\site-packages\django\template\base.py", line 175, in render
    return self._render(context)
           ^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\Nigel\site\env\Lib\site-packages\django\template\base.py", line 167, in _render
    return self.nodelist.render(context)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\Nigel\site\env\Lib\site-packages\django\template\base.py", line 1005, in render
    return SafeString("".join([node.render_annotated(context) for node in self]))
                              ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\Nigel\site\env\Lib\site-packages\django\template\base.py", line 1005, in <listcomp>
    return SafeString("".join([node.render_annotated(context) for node in self]))
                               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\Nigel\site\env\Lib\site-packages\django\template\base.py", line 966, in render_annotated
    return self.render(context)
           ^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\Nigel\site\env\Lib\site-packages\django\template\loader_tags.py", line 157, in render
    return compiled_parent._render(context)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\Nigel\site\env\Lib\site-packages\django\template\base.py", line 167, in _render
    return self.nodelist.render(context)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\Nigel\site\env\Lib\site-packages\django\template\base.py", line 1005, in render
    return SafeString("".join([node.render_annotated(context) for node in self]))
                              ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\Nigel\site\env\Lib\site-packages\django\template\base.py", line 1005, in <listcomp>
    return SafeString("".join([node.render_annotated(context) for node in self]))
                               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\Nigel\site\env\Lib\site-packages\django\template\base.py", line 966, in render_annotated
    return self.render(context)
           ^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\Nigel\site\env\Lib\site-packages\django\template\loader_tags.py", line 157, in render
    return compiled_parent._render(context)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\Nigel\site\env\Lib\site-packages\django\template\base.py", line 167, in _render
    return self.nodelist.render(context)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\Nigel\site\env\Lib\site-packages\django\template\base.py", line 1005, in render
    return SafeString("".join([node.render_annotated(context) for node in self]))
                              ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\Nigel\site\env\Lib\site-packages\django\template\base.py", line 1005, in <listcomp>
    return SafeString("".join([node.render_annotated(context) for node in self]))
                               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\Nigel\site\env\Lib\site-packages\django\template\base.py", line 966, in render_annotated
    return self.render(context)
           ^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\Nigel\site\env\Lib\site-packages\django\template\loader_tags.py", line 157, in render
    return compiled_parent._render(context)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\Nigel\site\env\Lib\site-packages\django\template\base.py", line 167, in _render
    return self.nodelist.render(context)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\Nigel\site\env\Lib\site-packages\django\template\base.py", line 1005, in render
    return SafeString("".join([node.render_annotated(context) for node in self]))
                              ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\Nigel\site\env\Lib\site-packages\django\template\base.py", line 1005, in <listcomp>
    return SafeString("".join([node.render_annotated(context) for node in self]))
                               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\Nigel\site\env\Lib\site-packages\django\template\base.py", line 966, in render_annotated
    return self.render(context)
           ^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\Nigel\site\env\Lib\site-packages\django\template\loader_tags.py", line 63, in render
    result = block.nodelist.render(context)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\Nigel\site\env\Lib\site-packages\django\template\base.py", line 1005, in render
    return SafeString("".join([node.render_annotated(context) for node in self]))
                              ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\Nigel\site\env\Lib\site-packages\django\template\base.py", line 1005, in <listcomp>
    return SafeString("".join([node.render_annotated(context) for node in self]))
                               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\Nigel\site\env\Lib\site-packages\django\template\base.py", line 966, in render_annotated
    return self.render(context)
           ^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\Nigel\site\env\Lib\site-packages\django\template\loader_tags.py", line 63, in render
    result = block.nodelist.render(context)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\Nigel\site\env\Lib\site-packages\django\template\base.py", line 1005, in render
    return SafeString("".join([node.render_annotated(context) for node in self]))
                              ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\Nigel\site\env\Lib\site-packages\django\template\base.py", line 1005, in <listcomp>
    return SafeString("".join([node.render_annotated(context) for node in self]))
                               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\Nigel\site\env\Lib\site-packages\django\template\base.py", line 966, in render_annotated
    return self.render(context)
           ^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\Nigel\site\env\Lib\site-packages\django\template\loader_tags.py", line 63, in render
    result = block.nodelist.render(context)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\Nigel\site\env\Lib\site-packages\django\template\base.py", line 1005, in render
    return SafeString("".join([node.render_annotated(context) for node in self]))
                              ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\Nigel\site\env\Lib\site-packages\django\template\base.py", line 1005, in <listcomp>
    return SafeString("".join([node.render_annotated(context) for node in self]))
                               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\Nigel\site\env\Lib\site-packages\django\template\base.py", line 966, in render_annotated
    return self.render(context)
           ^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\Nigel\site\env\Lib\site-packages\django\template\base.py", line 1064, in render
    output = self.filter_expression.resolve(context)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\Nigel\site\env\Lib\site-packages\django\template\base.py", line 715, in resolve
    obj = self.var.resolve(context)
          ^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\Nigel\site\env\Lib\site-packages\django\template\base.py", line 847, in resolve
    value = self._resolve_lookup(context)
            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\Nigel\site\env\Lib\site-packages\django\template\base.py", line 914, in _resolve_lookup
    current = current()
              ^^^^^^^^^
  File "C:\Users\Nigel\site\env\Lib\site-packages\wagtail\admin\panels\base.py", line 317, in render_form_content
    return mark_safe(self.render_html() + self.render_missing_fields())
                     ^^^^^^^^^^^^^^^^^^
  File "C:\Users\Nigel\site\env\Lib\site-packages\wagtail\admin\ui\components.py", line 22, in render_html
    return template.render(context_data)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\Nigel\site\env\Lib\site-packages\django\template\backends\django.py", line 61, in render
    return self.template.render(context)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\Nigel\site\env\Lib\site-packages\django\template\base.py", line 175, in render
    return self._render(context)
           ^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\Nigel\site\env\Lib\site-packages\django\template\base.py", line 167, in _render
    return self.nodelist.render(context)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\Nigel\site\env\Lib\site-packages\django\template\base.py", line 1005, in render
    return SafeString("".join([node.render_annotated(context) for node in self]))
                              ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\Nigel\site\env\Lib\site-packages\django\template\base.py", line 1005, in <listcomp>
    return SafeString("".join([node.render_annotated(context) for node in self]))
                               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\Nigel\site\env\Lib\site-packages\django\template\base.py", line 966, in render_annotated
    return self.render(context)
           ^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\Nigel\site\env\Lib\site-packages\django\template\defaulttags.py", line 238, in render
    nodelist.append(node.render_annotated(context))
                    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\Nigel\site\env\Lib\site-packages\django\template\base.py", line 966, in render_annotated
    return self.render(context)
           ^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\Nigel\site\env\Lib\site-packages\django\template\defaulttags.py", line 321, in render
    return nodelist.render(context)
           ^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\Nigel\site\env\Lib\site-packages\django\template\base.py", line 1005, in render
    return SafeString("".join([node.render_annotated(context) for node in self]))
                              ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\Nigel\site\env\Lib\site-packages\django\template\base.py", line 1005, in <listcomp>
    return SafeString("".join([node.render_annotated(context) for node in self]))
                               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\Nigel\site\env\Lib\site-packages\django\template\base.py", line 966, in render_annotated
    return self.render(context)
           ^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\Nigel\site\env\Lib\site-packages\django\template\base.py", line 1064, in render
    output = self.filter_expression.resolve(context)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\Nigel\site\env\Lib\site-packages\django\template\base.py", line 715, in resolve
    obj = self.var.resolve(context)
          ^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\Nigel\site\env\Lib\site-packages\django\template\base.py", line 847, in resolve
    value = self._resolve_lookup(context)
            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\Nigel\site\env\Lib\site-packages\django\template\base.py", line 914, in _resolve_lookup
    current = current()
              ^^^^^^^^^
  File "C:\Users\Nigel\site\env\Lib\site-packages\wagtail\admin\ui\components.py", line 22, in render_html
    return template.render(context_data)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\Nigel\site\env\Lib\site-packages\django\template\backends\django.py", line 61, in render
    return self.template.render(context)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\Nigel\site\env\Lib\site-packages\django\template\base.py", line 175, in render
    return self._render(context)
           ^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\Nigel\site\env\Lib\site-packages\django\template\base.py", line 167, in _render
    return self.nodelist.render(context)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\Nigel\site\env\Lib\site-packages\django\template\base.py", line 1005, in render
    return SafeString("".join([node.render_annotated(context) for node in self]))
                              ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\Nigel\site\env\Lib\site-packages\django\template\base.py", line 1005, in <listcomp>
    return SafeString("".join([node.render_annotated(context) for node in self]))
                               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\Nigel\site\env\Lib\site-packages\django\template\base.py", line 966, in render_annotated
    return self.render(context)
           ^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\Nigel\site\env\Lib\site-packages\django\template\defaulttags.py", line 238, in render
    nodelist.append(node.render_annotated(context))
                    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\Nigel\site\env\Lib\site-packages\django\template\base.py", line 966, in render_annotated
    return self.render(context)
           ^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\Nigel\site\env\Lib\site-packages\wagtail\admin\templatetags\wagtailadmin_tags.py", line 1048, in render
    children = self.nodelist.render(context) if self.nodelist else ""
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\Nigel\site\env\Lib\site-packages\django\template\base.py", line 1005, in render
    return SafeString("".join([node.render_annotated(context) for node in self]))
                              ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\Nigel\site\env\Lib\site-packages\django\template\base.py", line 1005, in <listcomp>
    return SafeString("".join([node.render_annotated(context) for node in self]))
                               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\Nigel\site\env\Lib\site-packages\django\template\base.py", line 966, in render_annotated
    return self.render(context)
           ^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\Nigel\site\env\Lib\site-packages\django\template\library.py", line 237, in render
    output = self.func(*resolved_args, **resolved_kwargs)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\Nigel\site\env\Lib\site-packages\wagtail\admin\templatetags\wagtailadmin_tags.py", line 976, in component
    return obj.render_html(context)
           ^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\Nigel\site\env\Lib\site-packages\wagtail\admin\ui\components.py", line 17, in render_html
    context_data = self.get_context_data(parent_context)
                   ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\Nigel\site\env\Lib\site-packages\wagtail\admin\panels\title_field_panel.py", line 60, in get_context_data
    field.widget.attrs.update(**self.get_attrs())
                                ^^^^^^^^^^^^^^^^
  File "C:\Users\Nigel\site\env\Lib\site-packages\wagtail\admin\panels\title_field_panel.py", line 81, in get_attrs
    targets = [self.get_target_selector(target) for target in panel.targets]
              ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\Nigel\site\env\Lib\site-packages\wagtail\admin\panels\title_field_panel.py", line 81, in <listcomp>
    targets = [self.get_target_selector(target) for target in panel.targets]
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\Nigel\site\env\Lib\site-packages\wagtail\admin\panels\title_field_panel.py", line 128, in get_target_selector
    field = self.form[target]
            ^^^^^^^^^^^^^^^^^
  File "C:\Users\Nigel\site\env\Lib\site-packages\django\forms\forms.py", line 180, in __getitem__
    raise KeyError(
    ^

Exception Type: KeyError at /admin/pages/14/edit/
Exception Value: "Key 'slug' not found in 'BlogDetailPageForm'. Choices are: category, classes, comment_notifications, container_size, containerized, content, expire_at, go_live_at, publish_at, search_description, seo_title, site, title."

mfpqipee

mfpqipee1#

这并没有深入到你在这里显示的错误的底部。但是我建议你把 slugFieldPanel留在Page.promote_panels中,并带有一个read_only属性,这样用户就不能编辑它了。你也可以利用permissions属性,参见文档。

相关问题