Re: [Wagtail] TypeError: expected string or bytes-like object after switching from RichTextField to StreamField in

847 views
Skip to first unread message

Matthew Westcott

unread,
Aug 22, 2017, 4:55:28 AM8/22/17
to wag...@googlegroups.com
Hi Ben - I see you deleted your corresponding question on StackOverflow. Did you solve it?

Cheers,
- Matt

> On 22 Aug 2017, at 03:29, Ben Pateman <bpat...@gmail.com> wrote:
>
> Still in development, I decided to switch over the body field for one of my wagtail.Page models from RichTextField to StreamField. In the wagtail admin interface I can create pages using this model without a problem, and StreamField seems to work fine. However, when viewing the resultant page I get the following error:
>
> Template error:
> In template D:\Documents\Dropbox\Work\TravelSite\travelnews\travelnews\templates\base.html, error at line 14
>
> expected
> string or bytes-like object 4 :
>
>
> 5 :
>
>
> 6 : <!DOCTYPE html>
>
>
> 7 : <!--[if lt IE 7]> <html class="no-js lt-ie9 lt-ie8 lt-ie7"> <![endif]-->
>
>
> 8 : <!--[if IE 7]> <html class="no-js lt-ie9 lt-ie8"> <![endif]-->
>
>
> 9 : <!--[if IE 8]> <html class="no-js lt-ie9"> <![endif]-->
>
>
> 10 : <!--[if gt IE 8]><!--> <html class="no-js"> <!--<![endif]-->
>
>
> 11 : <head>
>
>
> 12 : <meta charset="utf-8" />
>
>
> 13 : <meta http-equiv="X-UA-Compatible" content="IE=edge" />
>
>
> 14 : <script src="https:/ /ajax.googleapis.com/ajax/libs/jquery/2.2.4/jquery.min.js"></script>
>
>
> 15 : {% load bootstrap3 %}
>
>
> 16 : {% bootstrap_css %}
>
>
> 17 : {% bootstrap_javascript %}
>
>
> 18 : {% bootstrap_messages %}
>
>
> 19 :
>
>
> 20 : <title>
>
>
> 21 : {% block title %}
>
>
> 22 : {% if self.seo_title %}{{ self.seo_title }}{% else %}{{ self.title }}{% endif %}
>
>
> 23 : {% endblock %}
>
>
> 24 : {% block title_suffix %}
>
>
> Traceback:
>
>
>
> File "C:\Users\Ben\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django\core\handlers\exception.py" in
> inner
>
> 41. response = get_response(request)
>
>
>
> File "C:\Users\Ben\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django\core\handlers\base.py" in
> _get_response
>
> 217. response = self.process_exception_by_middleware(e, request)
>
>
>
> File "C:\Users\Ben\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django\core\handlers\base.py" in
> _get_response
>
> 215. response = response.render()
>
>
>
> File "C:\Users\Ben\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django\template\response.py" in
> render
>
> 107. self.content = self.
> rendered_content
>
>
> File "C:\Users\Ben\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django\template\response.py" in
> rendered_content
>
> 84. content = template.render(context, self._request)
>
>
>
> File "C:\Users\Ben\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django\template\backends\django.py" in
> render
>
> 66. return self.template.render(context)
>
>
>
> File "C:\Users\Ben\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django\template\base.py" in
> render
>
> 207. return self._render(context)
>
>
>
> File "C:\Users\Ben\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django\template\base.py" in
> _render
>
> 199. return self.nodelist.render(context)
>
>
>
> File "C:\Users\Ben\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django\template\base.py" in
> render
>
> 990. bit = node.render_annotated(context)
>
>
>
> File "C:\Users\Ben\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django\template\base.py" in
> render_annotated
>
> 957. return self.render(context)
>
>
>
> File "C:\Users\Ben\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django\template\loader_tags.py" in
> render
>
> 177. return compiled_parent._render(context)
>
>
>
> File "C:\Users\Ben\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django\template\base.py" in
> _render
>
> 199. return self.nodelist.render(context)
>
>
>
> File "C:\Users\Ben\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django\template\base.py" in
> render
>
> 990. bit = node.render_annotated(context)
>
>
>
> File "C:\Users\Ben\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django\template\base.py" in
> render_annotated
>
> 957. return self.render(context)
>
>
>
> File "C:\Users\Ben\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django\template\loader_tags.py" in
> render
>
> 72. result = block.nodelist.render(context)
>
>
>
> File "C:\Users\Ben\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django\template\base.py" in
> render
>
> 990. bit = node.render_annotated(context)
>
>
>
> File "C:\Users\Ben\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django\template\base.py" in
> render_annotated
>
> 957. return self.render(context)
>
>
>
> File "C:\Users\Ben\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django\template\base.py" in
> render
>
> 1040. output = self.filter_expression.resolve(context)
>
>
>
> File "C:\Users\Ben\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django\template\base.py" in
> resolve
>
> 736. new_obj = func(obj, *arg_vals)
>
>
>
> File "C:\Users\Ben\AppData\Local\Programs\Python\Python36-32\lib\site-packages\wagtail\wagtailcore\templatetags\wagtailcore_tags.py" in
> richtext
>
> 73. html = expand_db_html(value)
>
>
>
> File "C:\Users\Ben\AppData\Local\Programs\Python\Python36-32\lib\site-packages\wagtail\wagtailcore\rich_text.py" in
> expand_db_html
>
> 180. html = FIND_A_TAG.sub(replace_a_tag, html)
>
>
>
> Exception Type: TypeError at /guide-paris/
> Exception Value: expected string or bytes-like object
> I initially thought it was a problem with database migrations, but have since dumped the whole test db and loaded up a fresh one, and the problem persists.
>
> This is the StreamField definition in my page model:
>
> body = StreamField([
>
>
> ('heading', blocks.CharBlock()),
>
>
> ('paragraph', blocks.RichTextBlock()),
>
>
> ('quote', blocks.BlockQuoteBlock()),
>
>
> ])
> Spent more time than I'd care to admit trying to get my head around why this is happening, so if anyone has any ideas on how to resolve this I'd be incredibly grateful!
>
>
> --
> You received this message because you are subscribed to the Google Groups "Wagtail support" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to wagtail+u...@googlegroups.com.
> To post to this group, send email to wag...@googlegroups.com.
> Visit this group at https://groups.google.com/group/wagtail.
> To view this discussion on the web, visit https://groups.google.com/d/msgid/wagtail/43f701ac-945d-45c3-a754-492d689f21cc%40googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.

Chris Wedgwood

unread,
Aug 28, 2017, 5:49:59 PM8/28/17
to Wagtail support
I got this error when I didn't have include_block before page body. I too spent much time scratching my head but then realised it was well documented to do this:)

{% include_block page.body %}

Simon Holland

unread,
Jul 23, 2018, 8:41:00 AM7/23/18
to Wagtail support
Hi,   I believe I am having a similar issue and also have spent way too long on this.  Basically I have run through the walkthrough on wagtail.io to create a blog_index_page and blog_page.  Now I get the error ...

expected string or bytes-like object

from this line of my base.html ..
{% if self.seo_title %}{{ self.seo_title }}{% else %}{{ self.title }}{% endif %}
on the blog index page.  I am also using StreamField in place of the RichTextField.

I tried inserting ..
{% include_block page.body %}
but I am not sure I understand how it should be used.

Could you offer any pointers as to where I might be going wrong?

Many thanks,
Simon

My wagtail core site using StreamField ...

Simon Holland

unread,
Jul 23, 2018, 6:48:25 PM7/23/18
to Wagtail support
The error persists even if I delete the line calling the page title.  It seems I should be calling StreamField blocks differently.  However I cannot get my head around how.

Matthew Westcott

unread,
Jul 23, 2018, 6:57:00 PM7/23/18
to wag...@googlegroups.com
Hi Simon,
Unfortunately error reporting is a bit buggy in the current version of Django - you may find that it's reporting the right line number on the wrong template. I'd suggest looking at the corresponding line number in your blog index template, and seeing if that's something more relevant...

Also, please can you include the full stack trace of the error?

Cheers,
- Matt
> To view this discussion on the web, visit https://groups.google.com/d/msgid/wagtail/ff391389-55f3-4f97-913b-a72d545ad256%40googlegroups.com.

Simon Holland

unread,
Jul 23, 2018, 8:40:40 PM7/23/18
to Wagtail support
Matt,

Thank you so much, I was still using a "|richtext" call to my StreamField in the Blog Index Template.  Really really appreciate your insight, it had nothing to do with where I was looking and I would NEVER have found it otherwise.

Many thanks again,
Simon

Simon Holland

unread,
Jul 23, 2018, 9:02:55 PM7/23/18
to Wagtail support
Here is the Full Stack Trace, in all the excitement I almost forgot.  (FYI, I am really loving StreamField, thanks for a great product) ...


Environment:


Request Method: GET

Django Version: 2.0.7
Python Version: 3.6.5
Installed Applications:
['home',
 'search',
 'blog',
 'wagtail.contrib.forms',
 'wagtail.contrib.redirects',
 'wagtail.embeds',
 'wagtail.sites',
 'wagtail.users',
 'wagtail.snippets',
 'wagtail.documents',
 'wagtail.images',
 'wagtail.search',
 'wagtail.admin',
 'wagtail.core',
 'modelcluster',
 'taggit',
 'django.contrib.admin',
 'django.contrib.auth',
 'django.contrib.contenttypes',
 'django.contrib.sessions',
 'django.contrib.messages',
 'django.contrib.staticfiles']
Installed Middleware:
['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',
 'django.middleware.security.SecurityMiddleware',
 'wagtail.core.middleware.SiteMiddleware',
 'wagtail.contrib.redirects.middleware.RedirectMiddleware']


Template error:
In template /mnt/c/Users/simon/OneDrive/dev/www/core/core/templates/base.html, error at line 11
   expected string or bytes-like object
   1 : {% load static wagtailuserbar %}
   2 : 
   3 : {% load wagtailcore_tags %}
   4 : 
   5 : <!DOCTYPE html>
   6 : <html class="no-js">
   7 :     <head>
   8 :         <meta charset="utf-8" />
   9 :         <title>
   10 :             {% block title %}
   11 :                  {% if self .seo_title %}{{ self.seo_title }}{% else %}{{ self.title }}{% endif %}
   12 :             {% endblock %}
   13 :             {% block title_suffix %}
   14 :                 {% with self.get_site.site_name as site_name %}
   15 :                     {% if site_name %}- {{ site_name }}{% endif %}
   16 :                 {% endwith %}
   17 :             {% endblock %}
   18 :         </title>
   19 :         <meta name="description" content="" />
   20 :         <meta name="viewport" content="width=device-width, initial-scale=1" />
   21 : 


Traceback:

File "/mnt/c/Users/simon/OneDrive/dev/www/core/env/lib/python3.6/site-packages/django/core/handlers/exception.py" in inner
  35.             response = get_response(request)

File "/mnt/c/Users/simon/OneDrive/dev/www/core/env/lib/python3.6/site-packages/django/core/handlers/base.py" in _get_response
  158.                 response = self.process_exception_by_middleware(e, request)

File "/mnt/c/Users/simon/OneDrive/dev/www/core/env/lib/python3.6/site-packages/django/core/handlers/base.py" in _get_response
  156.                 response = response.render()

File "/mnt/c/Users/simon/OneDrive/dev/www/core/env/lib/python3.6/site-packages/django/template/response.py" in render
  106.             self.content = self.rendered_content

File "/mnt/c/Users/simon/OneDrive/dev/www/core/env/lib/python3.6/site-packages/django/template/response.py" in rendered_content
  83.         content = template.render(context, self._request)

File "/mnt/c/Users/simon/OneDrive/dev/www/core/env/lib/python3.6/site-packages/django/template/backends/django.py" in render
  61.             return self.template.render(context)

File "/mnt/c/Users/simon/OneDrive/dev/www/core/env/lib/python3.6/site-packages/django/template/base.py" in render
  175.                     return self._render(context)

File "/mnt/c/Users/simon/OneDrive/dev/www/core/env/lib/python3.6/site-packages/django/template/base.py" in _render
  167.         return self.nodelist.render(context)

File "/mnt/c/Users/simon/OneDrive/dev/www/core/env/lib/python3.6/site-packages/django/template/base.py" in render
  943.                 bit = node.render_annotated(context)

File "/mnt/c/Users/simon/OneDrive/dev/www/core/env/lib/python3.6/site-packages/django/template/base.py" in render_annotated
  910.             return self.render(context)

File "/mnt/c/Users/simon/OneDrive/dev/www/core/env/lib/python3.6/site-packages/django/template/loader_tags.py" in render
  155.             return compiled_parent._render(context)

File "/mnt/c/Users/simon/OneDrive/dev/www/core/env/lib/python3.6/site-packages/django/template/base.py" in _render
  167.         return self.nodelist.render(context)

File "/mnt/c/Users/simon/OneDrive/dev/www/core/env/lib/python3.6/site-packages/django/template/base.py" in render
  943.                 bit = node.render_annotated(context)

File "/mnt/c/Users/simon/OneDrive/dev/www/core/env/lib/python3.6/site-packages/django/template/base.py" in render_annotated
  910.             return self.render(context)

File "/mnt/c/Users/simon/OneDrive/dev/www/core/env/lib/python3.6/site-packages/django/template/loader_tags.py" in render
  67.                 result = block.nodelist.render(context)

File "/mnt/c/Users/simon/OneDrive/dev/www/core/env/lib/python3.6/site-packages/django/template/base.py" in render
  943.                 bit = node.render_annotated(context)

File "/mnt/c/Users/simon/OneDrive/dev/www/core/env/lib/python3.6/site-packages/django/template/base.py" in render_annotated
  910.             return self.render(context)

File "/mnt/c/Users/simon/OneDrive/dev/www/core/env/lib/python3.6/site-packages/django/template/base.py" in render
  993.             output = self.filter_expression.resolve(context)

File "/mnt/c/Users/simon/OneDrive/dev/www/core/env/lib/python3.6/site-packages/django/template/base.py" in resolve
  703.                 new_obj = func(obj, *arg_vals)

File "/mnt/c/Users/simon/OneDrive/dev/www/core/env/lib/python3.6/site-packages/wagtail/core/templatetags/wagtailcore_tags.py" in richtext
  58.         html = expand_db_html(value)

File "/mnt/c/Users/simon/OneDrive/dev/www/core/env/lib/python3.6/site-packages/wagtail/core/rich_text/__init__.py" in expand_db_html
  30.     return FRONTEND_REWRITER(html)

File "/mnt/c/Users/simon/OneDrive/dev/www/core/env/lib/python3.6/site-packages/wagtail/core/rich_text/rewriters.py" in __call__
  81.             html = rewrite(html)

File "/mnt/c/Users/simon/OneDrive/dev/www/core/env/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 /blog/
Exception Value: expected string or bytes-like object


Reply all
Reply to author
Forward
0 new messages