﻿id	summary	reporter	owner	description	type	status	component	version	severity	resolution	keywords	cc	stage	has_patch	needs_docs	needs_tests	needs_better_patch	easy	ui_ux
34220	Cannot import csrf_input_lazy from partially initialized module.	thommy	rajdesai24	"Hi,

most of the time a TemplateView using Jinja2 is working fine, but sometimes I get the following exception. I wasn't able to figure out why it happens:

{{{
ImportError: cannot import name 'csrf_input_lazy' from partially initialized module 'django.template.backends.utils' (most likely due to a circular import) (/srv/pydl/env/lib/python3.9/site-packages/django/template/backends/utils.py)
  File ""django/core/handlers/exception.py"", line 55, in inner
    response = get_response(request)
  File ""django/core/handlers/base.py"", line 220, in _get_response
    response = response.render()
  File ""django/template/response.py"", line 114, in render
    self.content = self.rendered_content
  File ""django/template/response.py"", line 92, in rendered_content
    return template.render(context, self._request)
  File ""django/template/backends/jinja2.py"", line 65, in render
    from .utils import csrf_input_lazy, csrf_token_lazy
}}}

The view looks like this:
{{{
class ScriptView(TemplateView):
    """"""View for Tcl scripts""""""

    template_engine = ""jinja2""
    content_type = ""text/x-tcl""

    extra_context = {""fqdn"": settings.FQDN}
}}}

And it is used like the following:
{{{
    path(
        ""script.tcl"",
        views.ScriptView.as_view(template_name=""script.tcl.j2""),
        name=""script"",
    ),
}}}

Is it a race condition in conjunction with the Jinja2 template backend or is TemplateView not supposed to be used like this?

Is it possible to force pre-loading the required modules?

Best regards
Thommy"	Bug	closed	Template system	4.1	Normal	fixed	Jinja2 TemplateView ImportError csrf_input_lazy		Ready for checkin	1	0	0	0	0	0
