鹡鸰姜戈
class AboutPage(Page):
header_image = ImageChooserBlock()
body = blocks.StreamBlock([
('title', blocks.CharBlock()),
('content', blocks.RichTextBlock()),
])
content_panels = Page.content_panels + [
FieldPanel('header_image'),
FieldPanel('body'),
]
我不断收到此错误,我什至无法开始理解如何调试
Exception in thread django-main-thread:
Traceback (most recent call last):
File "/usr/lib/python3.10/threading.py", line 1016, in _bootstrap_inner
self.run()
File "/usr/lib/python3.10/threading.py", line 953, in run
self._target(*self._args, **self._kwargs)
File "/home/khophi/Development/Photograph/venv/lib/python3.10/site-packages/django/utils/autoreload.py", line 64, in wrapper
fn(*args, **kwargs)
File "/home/khophi/Development/Photograph/venv/lib/python3.10/site-packages/django/core/management/commands/runserver.py", line 133, in inner_run
self.check(display_num_errors=True)
File "/home/khophi/Development/Photograph/venv/lib/python3.10/site-packages/django/core/management/base.py", line 485, in check
all_issues = checks.run_checks(
File "/home/khophi/Development/Photograph/venv/lib/python3.10/site-packages/django/core/checks/registry.py", line 88, in run_checks
new_errors = check(app_configs=app_configs, databases=databases)
File "/home/khophi/Development/Photograph/venv/lib/python3.10/site-packages/wagtail/admin/checks.py", line 70, in get_form_class_check
if not issubclass(edit_handler.get_form_class(), WagtailAdminPageForm):
File "/home/khophi/Development/Photograph/venv/lib/python3.10/site-packages/wagtail/admin/panels/base.py", line 134, in get_form_class
return get_form_for_model(
File "/home/khophi/Development/Photograph/venv/lib/python3.10/site-packages/wagtail/admin/panels/base.py", line 48, in get_form_for_model
return metaclass(class_name, tuple(bases), form_class_attrs)
File "/home/khophi/Development/Photograph/venv/lib/python3.10/site-packages/permissionedforms/forms.py", line 30, in __new__
new_class = super().__new__(mcs, name, bases, attrs)
File "/home/khophi/Development/Photograph/venv/lib/python3.10/site-packages/modelcluster/forms.py", line 259, in __new__
new_class = super().__new__(cls, name, bases, attrs)
File "/home/khophi/Development/Photograph/venv/lib/python3.10/site-packages/django/forms/models.py", line 321, in __new__
raise FieldError(message)
django.core.exceptions.FieldError: Unknown field(s) (body, header_image) specified for AboutPage
9:43
任何有 Wagtail Django 经验的人都应该提供帮助
诸如
ImageChooserBlock
和 StreamBlock
之类的块只能在 StreamField 中使用 - 它们与模型字段不同,并且在需要模型字段定义的地方使用它们将不起作用。
在这种情况下,正确的等价物是图像模型的外键和 StreamField:
from django.db import models
from wagtail.fields import StreamField
class AboutPage(Page):
header_image = models.ForeignKey(
"wagtailimages.Image", null=True, blank=True,
on_delete=models.SET_NULL, related_name="+"
)
body = StreamField([
('title', blocks.CharBlock()),
('content', blocks.RichTextBlock()),
])
content_panels = Page.content_panels + [
FieldPanel('header_image'),
FieldPanel('body'),
]