最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

json - Issue Updating a Django 1.11.8 Project to Django 5.1.7 — Webpack Loader Error - Stack Overflow

programmeradmin4浏览0评论

I've inherited a project initially developed in Django 1.11.8. Unfortunately, the original team left without providing any documentation, and there are minimal comments in the code. I'm currently updating the project to Django 5.1.7 and managed to resolve most issues, but I've hit a roadblock with Webpack Loader.

I'm encountering the following error during template rendering:

Error during template rendering
In template /home/alejandro/new-dell/wfp_voluntariado/pae_source/new_refact_version/app/partner/templates/schools/admin.html, error at line 54 expected string or bytes-like object, got 'dict'

Relevant code in the template (admin.html):

{% block js %}
    {{ block.super }}
    <script src="{% static 'js/plugins/toastr/toastr.min.js' %}"></script>
    <script type="text/javascript">
        var context = {
            departments: {{ departments|safe }},
            csrf_token: "{{ csrf_token }}",
            height: "100%",
            urls: {
                "get_municipalities": "{% url 'api_v1:get_municipalities' %}"
            }
        }
    </script>
    
    {% render_bundle 'vendor' 'js' 'PARTNER' %}
    {% render_bundle 'partner/PartnerSchoolsApp' 'js' 'PARTNER' %}
{% endblock %}

Class-based view (PartnerSchoolsRegisterView):

class PartnerSchoolsRegisterView(AjaxResponseMixin, JSONResponseMixin, TemplateView):
    template_name = 'schools/admin.html'
    partner = None

    def dispatch(self, request, *args, **kwargs):
        self.partner = request.partner
        return super().dispatch(request, *args, **kwargs)

    def get_context_data(self, **kwargs):
        ctx = super().get_context_data(**kwargs)
        departamentos = [{
            'pk': pc.pk,
            'code': pc.code,
            'name': u'{0} | {1}'.format(pc.code, pc.name)
        } for pc in Departamento.objects.all()]

        ctx.update(
            departments=json.dumps(departamentos),
            regions_url=reverse('administration:get_regions'),
        )
        print(ctx)
        return ctx

What I've Tried

  • Verified the context data using print(ctx) — it seems to output a valid JSON string.
  • Ensured that Webpack Loader is properly installed and configured. Attempted to isolate the issue by simplifying the template.

Based on the error message, it seems like Webpack Loader may be receiving a dictionary (dict) instead of a string or a valid path for the JavaScript bundle.

Is there an issue with the way I'm using {% render_bundle %} in the updated Django version? Could there be a problem with how the context variable departments is being serialized as JSON? Is there a more appropriate approach to handle Webpack bundles in Django 5.1.7?

发布评论

评论列表(0)

  1. 暂无评论