#34692 closed Bug (fixed)

django.forms.renderers.get_default_renderer()'s template loader cache is not being reset on autoloads.

Reported by: Andrew Owned by: Priyank Panchal
Component: Forms Version: 4.2
Severity: Normal Keywords:
Cc: David Smith 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 (last modified by Andrew)

Whenever a template file changes,
django.template.autoreload.template_changed() is called, which in turn calls
django.template.autoreload.reset_loaders(), which clears the template cache in every backend.engine's template loaders.

However, Django's renderer for Forms uses forms.renderers.get_default_renderer(), which returns a mixin that has its own engine instance, and therefore its own loaders (and template caches). This engine is never reset on autoloads. This means that any changes to a template that is referenced as part of, EG "Form.template_name", is never refreshed on changes. The entire runserver process must be restarted for any templates cached through forms.renders to be reloaded. This is tedious and does not match the behavior seen by non-form cached templates.

A crude fix can be demonstrated by changing reset_loaders() to the following:

def reset_loaders():
    for backend in engines.all():
        if not isinstance(backend, DjangoTemplates):
            continue
        for loader in backend.engine.template_loaders:
            loader.reset()

    # this code is new: reset the form renderer's template cache as well
    from django.forms.renderers import get_default_renderer
    backend = get_default_renderer().engine
    if isinstance(backend, DjangoTemplates):
        for loader in backend.engine.template_loaders:
            loader.reset()

Change History (13)

comment:1 by Andrew, 17 months ago

Description: modified (diff)
Summary: django.forms.get_default_renderer()'s template loader cache is not being reset on autoloads.django.forms.renderers.get_default_renderer()'s template loader cache is not being reset on autoloads.

comment:2 by Andrew, 17 months ago

Description: modified (diff)

comment:3 by Mariusz Felisiak, 17 months ago

Cc: David Smith added
Component: UncategorizedForms

comment:4 by Mariusz Felisiak, 17 months ago

Triage Stage: UnreviewedAccepted

Thanks for the report. I was able to reproduce this issue.

comment:5 by Amir Karimi, 16 months ago

It seems if we don't want to add some codes like the above, we have to change the flow of creation of the default renderer's engine. The default engine is not included in the

engines.all()

@MariuszFelisiak
Do you think it is worth changing the flow? I suggest letting it be solved in the simplest way.

in reply to:  5 ; comment:6 by Mariusz Felisiak, 16 months ago

Replying to Amir Karimi:

It seems if we don't want to add some codes like the above, we have to change the flow of creation of the default renderer's engine. The default engine is not included in the

engines.all()

@MariuszFelisiak
Do you think it is worth changing the flow? I suggest letting it be solved in the simplest way.

I'm not sure what are you proposing. We didn't reject any proposition.

comment:7 by Priyank Panchal, 16 months ago

Owner: changed from nobody to Priyank Panchal
Status: newassigned

in reply to:  6 comment:8 by Amir Karimi, 16 months ago

Replying to Mariusz Felisiak:

Replying to Amir Karimi:

It seems if we don't want to add some codes like the above, we have to change the flow of creation of the default renderer's engine. The default engine is not included in the

engines.all()

@MariuszFelisiak
Do you think it is worth changing the flow? I suggest letting it be solved in the simplest way.

I'm not sure what are you proposing. We didn't reject any proposition.

Yeah. I just wanted to think about its optimal solution. I concluded that maybe there is no cleaner way to solve this issue than simply coding as suggested.

comment:9 by Carlton Gibson, 16 months ago

Needs tests: set

comment:10 by Mariusz Felisiak, 16 months ago

Has patch: set

comment:11 by Mariusz Felisiak, 16 months ago

Needs tests: unset
Patch needs improvement: set

comment:12 by Mariusz Felisiak, 15 months ago

Patch needs improvement: unset
Triage Stage: AcceptedReady for checkin

comment:13 by Mariusz Felisiak <felisiak.mariusz@…>, 15 months ago

Resolution: fixed
Status: assignedclosed

In 439242c5:

Fixed #34692 -- Made autoreloader reset cached template loader for default renderer.

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