Django Wagtail“预期的字符串或类似字节的对象”错误

时间:2018-12-15 08:17:47

标签: python django python-3.x django-models wagtail

我一直很难弄清楚到底是什么导致了我的代码中的错误。我的主页呈现出来,但是当我尝试打开子页面时,出现预期的字符串错误。任何调试此功能的帮助将不胜感激。

下面的跟踪:

    Internal Server Error: /resonate/page-2/
Traceback (most recent call last):
  File "/Users/saeahmad/.pyenv/versions/auxe_cms/lib/python3.6/site-packages/django/core/handlers/exception.py", line 34, in inner
    response = get_response(request)
  File "/Users/saeahmad/.pyenv/versions/auxe_cms/lib/python3.6/site-packages/django/core/handlers/base.py", line 156, in _get_response
    response = self.process_exception_by_middleware(e, request)
  File "/Users/saeahmad/.pyenv/versions/auxe_cms/lib/python3.6/site-packages/django/core/handlers/base.py", line 154, in _get_response
    response = response.render()
  File "/Users/saeahmad/.pyenv/versions/auxe_cms/lib/python3.6/site-packages/django/template/response.py", line 106, in render
    self.content = self.rendered_content
  File "/Users/saeahmad/.pyenv/versions/auxe_cms/lib/python3.6/site-packages/django/template/response.py", line 83, in rendered_content
    content = template.render(context, self._request)
  File "/Users/saeahmad/.pyenv/versions/auxe_cms/lib/python3.6/site-packages/django/template/backends/django.py", line 61, in render
    return self.template.render(context)
  File "/Users/saeahmad/.pyenv/versions/auxe_cms/lib/python3.6/site-packages/django/template/base.py", line 171, in render
    return self._render(context)
  File "/Users/saeahmad/.pyenv/versions/auxe_cms/lib/python3.6/site-packages/django/template/base.py", line 163, in _render
    return self.nodelist.render(context)
  File "/Users/saeahmad/.pyenv/versions/auxe_cms/lib/python3.6/site-packages/django/template/base.py", line 937, in render
    bit = node.render_annotated(context)
  File "/Users/saeahmad/.pyenv/versions/auxe_cms/lib/python3.6/site-packages/django/template/base.py", line 904, in render_annotated
    return self.render(context)
  File "/Users/saeahmad/.pyenv/versions/auxe_cms/lib/python3.6/site-packages/django/template/loader_tags.py", line 150, in render
    return compiled_parent._render(context)
  File "/Users/saeahmad/.pyenv/versions/auxe_cms/lib/python3.6/site-packages/django/template/base.py", line 163, in _render
    return self.nodelist.render(context)
  File "/Users/saeahmad/.pyenv/versions/auxe_cms/lib/python3.6/site-packages/django/template/base.py", line 937, in render
    bit = node.render_annotated(context)
  File "/Users/saeahmad/.pyenv/versions/auxe_cms/lib/python3.6/site-packages/django/template/base.py", line 904, in render_annotated
    return self.render(context)
  File "/Users/saeahmad/.pyenv/versions/auxe_cms/lib/python3.6/site-packages/django/template/loader_tags.py", line 62, in render
    result = block.nodelist.render(context)
  File "/Users/saeahmad/.pyenv/versions/auxe_cms/lib/python3.6/site-packages/django/template/base.py", line 937, in render
    bit = node.render_annotated(context)
  File "/Users/saeahmad/.pyenv/versions/auxe_cms/lib/python3.6/site-packages/django/template/base.py", line 904, in render_annotated
    return self.render(context)
  File "/Users/saeahmad/.pyenv/versions/auxe_cms/lib/python3.6/site-packages/django/template/loader_tags.py", line 62, in render
    result = block.nodelist.render(context)
  File "/Users/saeahmad/.pyenv/versions/auxe_cms/lib/python3.6/site-packages/django/template/base.py", line 937, in render
    bit = node.render_annotated(context)
  File "/Users/saeahmad/.pyenv/versions/auxe_cms/lib/python3.6/site-packages/django/template/base.py", line 904, in render_annotated
    return self.render(context)
  File "/Users/saeahmad/.pyenv/versions/auxe_cms/lib/python3.6/site-packages/django/template/base.py", line 987, in render
    output = self.filter_expression.resolve(context)
  File "/Users/saeahmad/.pyenv/versions/auxe_cms/lib/python3.6/site-packages/django/template/base.py", line 698, in resolve
    new_obj = func(obj, *arg_vals)
  File "/Users/saeahmad/.pyenv/versions/auxe_cms/lib/python3.6/site-packages/wagtail/core/templatetags/wagtailcore_tags.py", line 75, in richtext
    html = expand_db_html(value)
  File "/Users/saeahmad/.pyenv/versions/auxe_cms/lib/python3.6/site-packages/wagtail/core/rich_text/__init__.py", line 30, in expand_db_html
    return FRONTEND_REWRITER(html)
  File "/Users/saeahmad/.pyenv/versions/auxe_cms/lib/python3.6/site-packages/wagtail/core/rich_text/rewriters.py", line 81, in __call__
    html = rewrite(html)
  File "/Users/saeahmad/.pyenv/versions/auxe_cms/lib/python3.6/site-packages/wagtail/core/rich_text/rewriters.py", line 71, in __call__
    return FIND_A_TAG.sub(self.replace_tag, html)
TypeError: expected string or bytes-like object
    Django Version: 2.1.1
    Python Version: 3.6.6
    Installed Applications:
    ['accounts',
     'home',
     'search',
     'design_system',
     'wagtail.contrib.settings',
     'wagtail.contrib.forms',
     'wagtail.contrib.redirects',
     'wagtail.contrib.routable_page',
     'wagtail.contrib.modeladmin',
     'wagtailmenus',
     'wagtail.embeds',
     'wagtail.sites',
     'wagtail.users',
     'wagtail.snippets',
     'wagtail.documents',
     'wagtail.images',
     'wagtail.search',
     'dashboard',
     'wagtail.admin',
     'wagtail.core',
     'modelcluster',
     'taggit',
     'condensedinlinepanel',
     'django.contrib.admin',
     'django.contrib.auth',
     'django.contrib.contenttypes',
     'django.contrib.sessions',
     'django.contrib.messages',
     'django.contrib.staticfiles']
    Installed Middleware:
    ['django.middleware.security.SecurityMiddleware',
     'django.contrib.sessions.middleware.SessionMiddleware',
     'django.middleware.common.CommonMiddleware',
     'django.middleware.csrf.CsrfViewMiddleware',
     'django.contrib.auth.middleware.AuthenticationMiddleware',
     'django.contrib.messages.middleware.MessageMiddleware',
     'django.middleware.clickjacking.XFrameOptionsMiddleware',
     'wagtail.core.middleware.SiteMiddleware',
     'wagtail.contrib.redirects.middleware.RedirectMiddleware']


    Template error:
    In template /Users/saeahmad/Desktop/code/auxe/AUXE_Design_System/auxe_cms/design_system/templates/design_system/base.html, error at line 0
       expected string or bytes-like object
       1 : {% load static wagtailcore_tags wagtailimages_tags menu_tags %}
       2 : <!DOCTYPE html>
       3 : <html lang="en">
       4 : 
       5 : <head>
       6 :     <meta charset="utf-8">
       7 :     <meta http-equiv="X-UA-Compatible" content="IE=edge">
       8 :     <meta name="viewport" content="width=device-width, initial-scale=1">
       9 : 
       10 :     <title>{% block title %}{{ main_page.title }}{% if main_page.description %} | {{ main_page.description }}{% endif %}{% endblock title %}</title>


    Traceback:

    File "/Users/saeahmad/.pyenv/versions/auxe_cms/lib/python3.6/site-packages/django/core/handlers/exception.py" in inner
      34.             response = get_response(request)

    File "/Users/saeahmad/.pyenv/versions/auxe_cms/lib/python3.6/site-packages/django/core/handlers/base.py" in _get_response
      156.                 response = self.process_exception_by_middleware(e, request)

    File "/Users/saeahmad/.pyenv/versions/auxe_cms/lib/python3.6/site-packages/django/core/handlers/base.py" in _get_response
      154.                 response = response.render()

    File "/Users/saeahmad/.pyenv/versions/auxe_cms/lib/python3.6/site-packages/django/template/response.py" in render
      106.             self.content = self.rendered_content

    File "/Users/saeahmad/.pyenv/versions/auxe_cms/lib/python3.6/site-packages/django/template/response.py" in rendered_content
      83.         content = template.render(context, self._request)

    File "/Users/saeahmad/.pyenv/versions/auxe_cms/lib/python3.6/site-packages/django/template/backends/django.py" in render
      61.             return self.template.render(context)

    File "/Users/saeahmad/.pyenv/versions/auxe_cms/lib/python3.6/site-packages/django/template/base.py" in render
      171.                     return self._render(context)

    File "/Users/saeahmad/.pyenv/versions/auxe_cms/lib/python3.6/site-packages/django/template/base.py" in _render
      163.         return self.nodelist.render(context)

    File "/Users/saeahmad/.pyenv/versions/auxe_cms/lib/python3.6/site-packages/django/template/base.py" in render
      937.                 bit = node.render_annotated(context)

    File "/Users/saeahmad/.pyenv/versions/auxe_cms/lib/python3.6/site-packages/django/template/base.py" in render_annotated
      904.             return self.render(context)

    File "/Users/saeahmad/.pyenv/versions/auxe_cms/lib/python3.6/site-packages/django/template/loader_tags.py" in render
      150.             return compiled_parent._render(context)

    File "/Users/saeahmad/.pyenv/versions/auxe_cms/lib/python3.6/site-packages/django/template/base.py" in _render
      163.         return self.nodelist.render(context)

    File "/Users/saeahmad/.pyenv/versions/auxe_cms/lib/python3.6/site-packages/django/template/base.py" in render
      937.                 bit = node.render_annotated(context)

    File "/Users/saeahmad/.pyenv/versions/auxe_cms/lib/python3.6/site-packages/django/template/base.py" in render_annotated
      904.             return self.render(context)

    File "/Users/saeahmad/.pyenv/versions/auxe_cms/lib/python3.6/site-packages/django/template/loader_tags.py" in render
      62.                 result = block.nodelist.render(context)

    File "/Users/saeahmad/.pyenv/versions/auxe_cms/lib/python3.6/site-packages/django/template/base.py" in render
      937.                 bit = node.render_annotated(context)

    File "/Users/saeahmad/.pyenv/versions/auxe_cms/lib/python3.6/site-packages/django/template/base.py" in render_annotated
      904.             return self.render(context)

    File "/Users/saeahmad/.pyenv/versions/auxe_cms/lib/python3.6/site-packages/django/template/loader_tags.py" in render
      62.                 result = block.nodelist.render(context)

    File "/Users/saeahmad/.pyenv/versions/auxe_cms/lib/python3.6/site-packages/django/template/base.py" in render
      937.                 bit = node.render_annotated(context)

    File "/Users/saeahmad/.pyenv/versions/auxe_cms/lib/python3.6/site-packages/django/template/base.py" in render_annotated
      904.             return self.render(context)

    File "/Users/saeahmad/.pyenv/versions/auxe_cms/lib/python3.6/site-packages/django/template/base.py" in render
      987.             output = self.filter_expression.resolve(context)

    File "/Users/saeahmad/.pyenv/versions/auxe_cms/lib/python3.6/site-packages/django/template/base.py" in resolve
      698.                 new_obj = func(obj, *arg_vals)

    File "/Users/saeahmad/.pyenv/versions/auxe_cms/lib/python3.6/site-packages/wagtail/core/templatetags/wagtailcore_tags.py" in richtext
      75.         html = expand_db_html(value)

    File "/Users/saeahmad/.pyenv/versions/auxe_cms/lib/python3.6/site-packages/wagtail/core/rich_text/__init__.py" in expand_db_html
      30.     return FRONTEND_REWRITER(html)

    File "/Users/saeahmad/.pyenv/versions/auxe_cms/lib/python3.6/site-packages/wagtail/core/rich_text/rewriters.py" in __call__
      81.             html = rewrite(html)

    File "/Users/saeahmad/.pyenv/versions/auxe_cms/lib/python3.6/site-packages/wagtail/core/rich_text/rewriters.py" in __call__
      71.         return FIND_A_TAG.sub(self.replace_tag, html)

    Exception Type: TypeError at /resonate/page-2/
    Exception Value: expected string or bytes-like object

我也浏览了我的base.html文件,但似乎找不到任何错误,因此我认为内容页面的模型中可能有此错误?

class ContentPage(Page):
    body = StreamField([
        ('heading', blocks.CharBlock(classname="full title")),
        ('paragraph', blocks.RichTextBlock()),
        ('rich_text', blocks.RichTextBlock(icon='doc-full', label='Rich Text')),
        ('two_columns', TwoColumnBlock()),
        ('image', ImageChooserBlock(icon="image")),
        ('embedded_video', EmbedBlock(icon="media")),
        ('html', blocks.RawHTMLBlock(icon='site', label='HTML')),

    ],null=True,blank=True)

    content_panels = Page.content_panels + [
        StreamFieldPanel('body'),
    ]

    @property
    def main_page(self):
        return self.get_parent().specific

    def get_context(self, request, *args, **kwargs):
        context = super(ContentPage, self).get_context(request, *args, **kwargs)
        context['main_page'] = self.main_page
        context['content'] = self
        return context

1 个答案:

答案 0 :(得分:2)

堆栈跟踪表明您正在将无效的非字符串值传递给模板上某个地方的|richtext过滤器。允许的值为字符串None和StreamField RichTextBlocks中使用的特殊wagtail.core.rich_text.RichText类型。

(不幸的是,a current Django bug阻止在模板中显示错误的正确位置,因此没有立即找到哪个特定|richtext标签失败的方法-我建议暂时删除选定的标签直到找到受影响的代码。)