Opened 2 years ago
Closed 2 years ago
#34220 closed Bug (fixed)
Cannot import csrf_input_lazy from partially initialized module.
Reported by: | thommy | Owned by: | rajdesai24 |
---|---|---|---|
Component: | Template system | Version: | 4.1 |
Severity: | Normal | Keywords: | Jinja2 TemplateView ImportError csrf_input_lazy |
Cc: | Triage Stage: | Ready for checkin | |
Has patch: | yes | Needs documentation: | no |
Needs tests: | no | Patch needs improvement: | no |
Easy pickings: | no | UI/UX: | no |
Description
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
Change History (11)
comment:1 by , 2 years ago
Summary: | ImportError: cannot import name 'csrf_input_lazy' from partially initialized module 'django.template.backends.utils' → Cannot import csrf_input_lazy from partially initialized module. |
---|---|
Triage Stage: | Unreviewed → Accepted |
comment:2 by , 2 years ago
Hi Mariusz,
thanks for the quick response. I'll give it a try.
However it might take a couple of days to be sure because the problem happens so rarely.
Best regards
Thommy
follow-up: 4 comment:3 by , 2 years ago
Hi Mariusz,
happy new year!
So far the exception hasn't occured again.
It would be great to see this patch in one of the next bugfix releases :-)
Best regards
Thommy
comment:4 by , 2 years ago
Replying to thommy:
Hi Mariusz,
happy new year!
The same to you!
So far the exception hasn't occured again.
That's great! Would you like to prepare PR?
It would be great to see this patch in one of the next bugfix releases :-)
Per our backporting policy it doesn't qualify for a backport, so it can be fixed in Django 4.2, see Django’s release process for more details.
comment:6 by , 2 years ago
Replying to rajdesai24:
hey Mariusz would love to draft a PR
Thanks, please prepare PR, see my comment. There is nothing more to do, IMO.
comment:7 by , 2 years ago
Owner: | changed from | to
---|---|
Status: | new → assigned |
comment:9 by , 2 years ago
Has patch: | set |
---|---|
Triage Stage: | Accepted → Ready for checkin |
Tentatively accepted (similar to the #33420). Can you confirm that the following patch works for you:
django/template/backends/jinja2.py
from .utils import csrf_input_lazy, csrf_token_lazy