Opened 2 years ago

Closed 23 months 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 Mariusz Felisiak, 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: UnreviewedAccepted

Tentatively accepted (similar to the #33420). Can you confirm that the following patch works for you:

  • django/template/backends/jinja2.py

    diff --git a/django/template/backends/jinja2.py b/django/template/backends/jinja2.py
    index 199d62b429..031278c2e2 100644
    a b from django.utils.functional import cached_property  
    88from django.utils.module_loading import import_string
    99
    1010from .base import BaseEngine
     11from .utils import csrf_input_lazy, csrf_token_lazy
    1112
    1213
    1314class Jinja2(BaseEngine):
    class Template:  
    6263        )
    6364
    6465    def render(self, context=None, request=None):
    65         from .utils import csrf_input_lazy, csrf_token_lazy
    66 
    6766        if context is None:
    6867            context = {}
    6968        if request is not None:

comment:2 by thommy, 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

comment:3 by thommy, 23 months 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

in reply to:  3 comment:4 by Mariusz Felisiak, 23 months 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:5 by rajdesai24, 23 months ago

hey Mariusz would love to draft a PR

in reply to:  5 comment:6 by Mariusz Felisiak, 23 months 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 rajdesai24, 23 months ago

Owner: changed from nobody to rajdesai24
Status: newassigned

comment:8 by rajdesai24, 23 months ago

Please find the PR

comment:9 by Mariusz Felisiak, 23 months ago

Has patch: set
Triage Stage: AcceptedReady for checkin

comment:10 by thommy, 23 months ago

Thank you :-)

comment:11 by GitHub <noreply@…>, 23 months ago

Resolution: fixed
Status: assignedclosed

In d5f892d8:

Fixed #34220 -- Moved csrf_input_lazy, csrf_token_lazy imports to the toplevel.

This prevents random errors with partially initialized modules.

Note: See TracTickets for help on using tickets.
Back to Top