user3043817
user3043817

Reputation: 71

Django view - template returned as bytestring (b'')

i have a strange problem with Django templates after merging recent changes.
I migrated my application from Django 2.0.5 to 2.1.7, had some places in code to correct but nothing related with existing templates.

After rebuild and deploy my templates are returned as bytestring in HTTP response body:
b'<!DOCTYPE html>\n\n<html lang="en">\n <head ...

Django settings charset is set to default utf-8 (i have some polish signs in page body) and files are encoded as: text/html; charset=us-ascii
As far as i know us-ascii is subset of UTF-8 so it should be good.

My base template is visible by template loader under common_app/base.html:

<!DOCTYPE html>
{% load pipeline navigation_tags i18n %}
<html lang="en">
    <head>
        <meta charset="utf-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge">
        <meta name="viewport" content="width=device-width, initial-scale=1">
        <title>XXX {% block title %}{% endblock title %}</title>
        {% stylesheet 'common' %}
        {% javascript 'common' %}
        {% block header %}
        {% endblock %}
        {% stylesheet 'sb-admin' %}
        {% javascript 'sb-admin' %}
        ...
</head>
<body>
    {%  block body %}
            ...
            {% if user.is_authenticated %}
                    ...
                    {% render_navigation %}
                ...
            {% endif %}
            ...
            {% if error %}
            ...
            {% else %}
                {% block content %}
                {% endblock %}
            {% endif %}
        ...
        {% block footer %}
        {% endblock footer %}
        ...
    {% endblock body %}
</body>
</html>

And first visible page is handled like that:
ulrs.py:

urlpatterns = [
    path('', views.index, name='index'),
    .... ]

views.py:

@require_http_methods(["GET"])
def index(request):
    response = {
        'arg1' : 'arg1'
        ...
    }
    return render(request, 'app1/index.html', response)

And app1/index.html:

{% extends 'common_app/base.html' %}
{% load pipeline i18n %}
{% load humanize %}

{% block title %}- subtitle{% endblock title %}

{% block header %}
    some js etc...
{% endblock header %}


{% block content %}
    content

{% endblock content %}

{% block footer %}
    {% javascript 'somescript' %}
{% endblock footer %}

I guess that because of that b'' return page gets rendered entirely in <body> without populating appropriate blocks:

<html lang="en">
  <head></head>
  <body>
    "b'\n\n\n"
    <meta charset="utf-8">
    rest of head block...
    rest of content block ...
  </body>
</html>

Every view is returned as that - b'...'

What is even more strange this behavior only occurs on my production enviroment. Application itself is served in container with nginx. But nothing has changed beside of django version in container.
When i build it locally everything works as expected.

Upvotes: 1

Views: 153

Answers (1)

drosenberger
drosenberger

Reputation: 31

Based on your common_app/base.html file I can see that you're using django-pipeline.

I had the same issue after upgrading to Django 2.2.22 while still using an the old version 1.6.4 of django-pipeline.

Upgrading django-pipeline to the latest available version (currently 2.0.6) solved the issue you described above.

Upvotes: 1

Related Questions