鹡鸰断言错误"不匹配的标记:预期的 img,得到 p"



我在Docker中使用Postgres数据库运行Wagtail 2.4,当我试图在Wagtail中编辑博客文章时出错。最初的帖子起了作用,我可以在网站上很好地查看页面。当我在Wagtail管理员中编辑帖子时,它会抛出一个500错误:AssertionError "Unmatched tags: expected img, got p"

我最初认为这个问题是因为博客文章最初是从另一个数据库(旧的Drupal网站(导入的,并且introbody字段包含图像标记,尽管简介根本不应该有任何HTML标记。然而,我使用Navicat在数据库中删除了它们,错误仍然存在。不知道还能尝试什么,但我可以确认,并不是所有的帖子都在编辑视图中出现错误,只是从Drupal导入的帖子。在Wagtail管理中添加的任何更新内容都可以。

模型如下:

class NewsPage(Page):    
main_image = models.ForeignKey('wagtailimages.Image', null=True, blank=True,
on_delete=models.SET_NULL, related_name='+')
date = models.DateTimeField(default=datetime.datetime.today)
category = models.CharField(max_length=50, choices=CATEGORIES)
intro = models.TextField()
body = RichTextField(blank=True)
search_keywords = models.TextField(blank=True, null=True)
search_fields = Page.search_fields + [
index.SearchField('intro'),
index.SearchField('body'),
]
content_panels = Page.content_panels + [
ImageChooserPanel('main_image'),
FieldPanel('category'),
FieldPanel('intro'),
FieldPanel('body', classname='full'),
]
promote_panels = Page.promote_panels + [
FieldPanel('search_keywords'),
]
settings_panels = Page.settings_panels + [
FieldPanel('date'),
]
@property
def news_index_page(self):
return self.get_parent().specific
def get_context(self, request, *args, **kwargs):
context = super(NewsPage, self).get_context(request, *args, **kwargs)
context['news_index_page'] = self.news_index_page
context['news'] = self
return context

追溯:

Traceback:
File "/usr/local/lib/python3.6/site-packages/django/core/handlers/exception.py" in inner
34.             response = get_response(request)
File "/usr/local/lib/python3.6/site-packages/django/core/handlers/base.py" in _get_response
115.                 response = self.process_exception_by_middleware(e, request)
File "/usr/local/lib/python3.6/site-packages/django/core/handlers/base.py" in _get_response
113.                 response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/usr/local/lib/python3.6/site-packages/django/views/decorators/cache.py" in _wrapped_view_func
44.         response = view_func(request, *args, **kwargs)
File "/usr/local/lib/python3.6/site-packages/wagtail/admin/urls/__init__.py" in wrapper
102.             return view_func(request, *args, **kwargs)
File "/usr/local/lib/python3.6/site-packages/wagtail/admin/decorators.py" in decorated_view
34.             return view_func(request, *args, **kwargs)
File "/usr/local/lib/python3.6/site-packages/wagtail/admin/views/pages.py" in edit
545.         'has_unsaved_changes': has_unsaved_changes,
File "/usr/local/lib/python3.6/site-packages/django/shortcuts.py" in render
36.     content = loader.render_to_string(template_name, context, request, using=using)
File "/usr/local/lib/python3.6/site-packages/django/template/loader.py" in render_to_string
62.     return template.render(context, request)
File "/usr/local/lib/python3.6/site-packages/django/template/backends/django.py" in render
61.             return self.template.render(context)
File "/usr/local/lib/python3.6/site-packages/django/template/base.py" in render
171.                     return self._render(context)
File "/usr/local/lib/python3.6/site-packages/django/template/base.py" in _render
163.         return self.nodelist.render(context)
File "/usr/local/lib/python3.6/site-packages/django/template/base.py" in render
937.                 bit = node.render_annotated(context)
File "/usr/local/lib/python3.6/site-packages/django/template/base.py" in render_annotated
904.             return self.render(context)
File "/usr/local/lib/python3.6/site-packages/django/template/loader_tags.py" in render
150.             return compiled_parent._render(context)
File "/usr/local/lib/python3.6/site-packages/django/template/base.py" in _render
163.         return self.nodelist.render(context)
File "/usr/local/lib/python3.6/site-packages/django/template/base.py" in render
937.                 bit = node.render_annotated(context)
File "/usr/local/lib/python3.6/site-packages/django/template/base.py" in render_annotated
904.             return self.render(context)
File "/usr/local/lib/python3.6/site-packages/django/template/loader_tags.py" in render
150.             return compiled_parent._render(context)
File "/usr/local/lib/python3.6/site-packages/django/template/base.py" in _render
163.         return self.nodelist.render(context)
File "/usr/local/lib/python3.6/site-packages/django/template/base.py" in render
937.                 bit = node.render_annotated(context)
File "/usr/local/lib/python3.6/site-packages/django/template/base.py" in render_annotated
904.             return self.render(context)
File "/usr/local/lib/python3.6/site-packages/django/template/loader_tags.py" in render
150.             return compiled_parent._render(context)
File "/usr/local/lib/python3.6/site-packages/django/template/base.py" in _render
163.         return self.nodelist.render(context)
File "/usr/local/lib/python3.6/site-packages/django/template/base.py" in render
937.                 bit = node.render_annotated(context)
File "/usr/local/lib/python3.6/site-packages/django/template/base.py" in render_annotated
904.             return self.render(context)
File "/usr/local/lib/python3.6/site-packages/django/template/loader_tags.py" in render
62.                 result = block.nodelist.render(context)
File "/usr/local/lib/python3.6/site-packages/django/template/base.py" in render
937.                 bit = node.render_annotated(context)
File "/usr/local/lib/python3.6/site-packages/django/template/base.py" in render_annotated
904.             return self.render(context)
File "/usr/local/lib/python3.6/site-packages/django/template/loader_tags.py" in render
62.                 result = block.nodelist.render(context)
File "/usr/local/lib/python3.6/site-packages/django/template/base.py" in render
937.                 bit = node.render_annotated(context)
File "/usr/local/lib/python3.6/site-packages/django/template/base.py" in render_annotated
904.             return self.render(context)
File "/usr/local/lib/python3.6/site-packages/django/template/base.py" in render
987.             output = self.filter_expression.resolve(context)
File "/usr/local/lib/python3.6/site-packages/django/template/base.py" in resolve
671.                 obj = self.var.resolve(context)
File "/usr/local/lib/python3.6/site-packages/django/template/base.py" in resolve
796.             value = self._resolve_lookup(context)
File "/usr/local/lib/python3.6/site-packages/django/template/base.py" in _resolve_lookup
858.                             current = current()
File "/usr/local/lib/python3.6/site-packages/wagtail/admin/edit_handlers.py" in render_form_content
252.         return mark_safe(self.render_as_object() + self.render_missing_fields())
File "/usr/local/lib/python3.6/site-packages/wagtail/admin/edit_handlers.py" in render_as_object
220.         return self.render()
File "/usr/local/lib/python3.6/site-packages/wagtail/admin/edit_handlers.py" in render
324.             'self': self
File "/usr/local/lib/python3.6/site-packages/django/template/loader.py" in render_to_string
62.     return template.render(context, request)
File "/usr/local/lib/python3.6/site-packages/django/template/backends/django.py" in render
61.             return self.template.render(context)
File "/usr/local/lib/python3.6/site-packages/django/template/base.py" in render
171.                     return self._render(context)
File "/usr/local/lib/python3.6/site-packages/django/template/base.py" in _render
163.         return self.nodelist.render(context)
File "/usr/local/lib/python3.6/site-packages/django/template/base.py" in render
937.                 bit = node.render_annotated(context)
File "/usr/local/lib/python3.6/site-packages/django/template/base.py" in render_annotated
904.             return self.render(context)
File "/usr/local/lib/python3.6/site-packages/django/template/defaulttags.py" in render
209.                     nodelist.append(node.render_annotated(context))
File "/usr/local/lib/python3.6/site-packages/django/template/base.py" in render_annotated
904.             return self.render(context)
File "/usr/local/lib/python3.6/site-packages/django/template/base.py" in render
987.             output = self.filter_expression.resolve(context)
File "/usr/local/lib/python3.6/site-packages/django/template/base.py" in resolve
671.                 obj = self.var.resolve(context)
File "/usr/local/lib/python3.6/site-packages/django/template/base.py" in resolve
796.             value = self._resolve_lookup(context)
File "/usr/local/lib/python3.6/site-packages/django/template/base.py" in _resolve_lookup
858.                             current = current()
File "/usr/local/lib/python3.6/site-packages/wagtail/admin/edit_handlers.py" in render_as_object
220.         return self.render()
File "/usr/local/lib/python3.6/site-packages/wagtail/admin/edit_handlers.py" in render
324.             'self': self
File "/usr/local/lib/python3.6/site-packages/django/template/loader.py" in render_to_string
62.     return template.render(context, request)
File "/usr/local/lib/python3.6/site-packages/django/template/backends/django.py" in render
61.             return self.template.render(context)
File "/usr/local/lib/python3.6/site-packages/django/template/base.py" in render
171.                     return self._render(context)
File "/usr/local/lib/python3.6/site-packages/django/template/base.py" in _render
163.         return self.nodelist.render(context)
File "/usr/local/lib/python3.6/site-packages/django/template/base.py" in render
937.                 bit = node.render_annotated(context)
File "/usr/local/lib/python3.6/site-packages/django/template/base.py" in render_annotated
904.             return self.render(context)
File "/usr/local/lib/python3.6/site-packages/django/template/defaulttags.py" in render
209.                     nodelist.append(node.render_annotated(context))
File "/usr/local/lib/python3.6/site-packages/django/template/base.py" in render_annotated
904.             return self.render(context)
File "/usr/local/lib/python3.6/site-packages/django/template/base.py" in render
987.             output = self.filter_expression.resolve(context)
File "/usr/local/lib/python3.6/site-packages/django/template/base.py" in resolve
671.                 obj = self.var.resolve(context)
File "/usr/local/lib/python3.6/site-packages/django/template/base.py" in resolve
796.             value = self._resolve_lookup(context)
File "/usr/local/lib/python3.6/site-packages/django/template/base.py" in _resolve_lookup
858.                             current = current()
File "/usr/local/lib/python3.6/site-packages/wagtail/admin/edit_handlers.py" in render_as_object
482.             'field': self.bound_field,
File "/usr/local/lib/python3.6/site-packages/django/template/loader.py" in render_to_string
62.     return template.render(context, request)
File "/usr/local/lib/python3.6/site-packages/django/template/backends/django.py" in render
61.             return self.template.render(context)
File "/usr/local/lib/python3.6/site-packages/django/template/base.py" in render
171.                     return self._render(context)
File "/usr/local/lib/python3.6/site-packages/django/template/base.py" in _render
163.         return self.nodelist.render(context)
File "/usr/local/lib/python3.6/site-packages/django/template/base.py" in render
937.                 bit = node.render_annotated(context)
File "/usr/local/lib/python3.6/site-packages/django/template/base.py" in render_annotated
904.             return self.render(context)
File "/usr/local/lib/python3.6/site-packages/django/template/loader_tags.py" in render
188.             return template.render(context)
File "/usr/local/lib/python3.6/site-packages/django/template/base.py" in render
173.                 return self._render(context)
File "/usr/local/lib/python3.6/site-packages/django/template/base.py" in _render
163.         return self.nodelist.render(context)
File "/usr/local/lib/python3.6/site-packages/django/template/base.py" in render
937.                 bit = node.render_annotated(context)
File "/usr/local/lib/python3.6/site-packages/django/template/base.py" in render_annotated
904.             return self.render(context)
File "/usr/local/lib/python3.6/site-packages/django/template/loader_tags.py" in render
53.                 result = self.nodelist.render(context)
File "/usr/local/lib/python3.6/site-packages/django/template/base.py" in render
937.                 bit = node.render_annotated(context)
File "/usr/local/lib/python3.6/site-packages/django/template/base.py" in render_annotated
904.             return self.render(context)
File "/usr/local/lib/python3.6/site-packages/django/template/base.py" in render
987.             output = self.filter_expression.resolve(context)
File "/usr/local/lib/python3.6/site-packages/django/template/base.py" in resolve
698.                 new_obj = func(obj, *arg_vals)
File "/usr/local/lib/python3.6/site-packages/wagtail/admin/templatetags/wagtailadmin_tags.py" in render_with_errors
251.         return bound_field.as_widget()
File "/usr/local/lib/python3.6/site-packages/django/forms/boundfield.py" in as_widget
93.             renderer=self.form.renderer,
File "/usr/local/lib/python3.6/site-packages/django/forms/widgets.py" in render
241.         context = self.get_context(name, value, attrs)
File "/usr/local/lib/python3.6/site-packages/wagtail/admin/rich_text/editors/draftail/__init__.py" in get_context
62.         context = super().get_context(name, value, attrs)
File "/usr/local/lib/python3.6/site-packages/django/forms/widgets.py" in get_context
293.         context = super().get_context(name, value, attrs)
File "/usr/local/lib/python3.6/site-packages/django/forms/widgets.py" in get_context
233.             'value': self.format_value(value),
File "/usr/local/lib/python3.6/site-packages/wagtail/admin/rich_text/editors/draftail/__init__.py" in format_value
59.         return self.converter.from_database_format(value)
File "/usr/local/lib/python3.6/site-packages/wagtail/admin/rich_text/converters/contentstate.py" in from_database_format
84.         self.html_to_contentstate_handler.feed(html)
File "/usr/local/lib/python3.6/html/parser.py" in feed
111.         self.goahead(0)
File "/usr/local/lib/python3.6/html/parser.py" in goahead
173.                     k = self.parse_endtag(i)
File "/usr/local/lib/python3.6/html/parser.py" in parse_endtag
421.         self.handle_endtag(elem.lower())
File "/usr/local/lib/python3.6/site-packages/wagtail/admin/rich_text/converters/html_to_contentstate.py" in handle_endtag
313.         assert name == expected_name, "Unmatched tags: expected %s, got %s" % (expected_name, name)
Exception Type: AssertionError at /admin/pages/2251/edit/
Exception Value: Unmatched tags: expected img, got p 

问题是Wagtail的解析器不能容忍格式错误的HTML,并且您从Drupal导入的HTML格式错误(例如,它包含未关闭的标记(。Wagtail编辑器中断这个功能有点烦人,因为你没有办法从编辑器中修复内容。

我过去所做的就是使用Beautiful Soup这样的HTML解析器来修复shell中糟糕的HTML。大致如下:

from bs4 import BeautifulSoup
for page in NewsPage.objects.all():
html = page.body
soup = BeautifulSoup(html, 'html5lib')
cleaned_html = soup.body.decode_contents()
page.body = cleaned_html
page.save()

decode_contents()将生成Wagtail的解析器不会抱怨的有效HTML。

我的解决方案最终工作如下:

from bs4 import BeautifulSoup
for page in NewsPage.objects.all():
html = page.body
soup = BeautifulSoup(html, "html.parser")
if soup.find("img"):
soup.img.decompose()
page.body = soup
page.save_revision().publish()

相关内容

  • 没有找到相关文章

最新更新