TemplateDoesNotExist for `forms_widgets` after migrating templates from app to project level

Hello, I migrated templates from per-app level to project level. Now after the migration, everything is working except the pages where I’m using custom checkboxes.

Do you have any idea what is wrong ?

(As said, everything else is working so as a workaround I quickly created again templates folder inside of app and copy-pasted this single file back.)

This is the custom checkbox definition inside of my forms.py:

class CustomCheckboxInput(forms.CheckboxInput):
    template_name = 'myapp/forms_widgets/checkbox_custom.html'

And here is TEMPLATES from my settings.py:

TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [os.path.join(BASE_DIR, 'templates')],
        'APP_DIRS': True,
        'OPTIONS': {
            'context_processors': [
                'django.template.context_processors.debug',
                'django.template.context_processors.request',
                'django.contrib.auth.context_processors.auth',
                'django.contrib.messages.context_processors.messages',
            ],
        },
    },
]

The error trace is also mentioning that the issue comes from my view function which looks like this:

def landing_page(request):
    ...
    return render(request, 'myapp/landing_page.html', context)

Yes, that’s the way it works for custom form and widget rendering.

See the conversation at Reusable form templates not working in Django 4.0?! along with the references at:

So you are saying I should just leave this things in the app-level templates folder ? Is that correct ?

If you want to move those assets to the project level, you can. But to do so, you also need to make the appropriate settings change. It’s your choice. I’m not making a recommendation one way or the other.

It may or may not be the right choice for you - I’m not in a position to be able to answer that.

Yes, of course, was more asking if this would be the best practice :slight_smile:
Thank you!

So for clarity, my answer would be that from that perspective, there’s no “one right answer”. I can envision specific situations where each is preferable, depending upon the overall architecture of the system.

1 Like