1024programmer Java Django widget template overrides won’t search in project templates directory. How to fix?

Django widget template overrides won’t search in project templates directory. How to fix?

I’m trying to override the built-in widget templates in Django 1.11. I seem to be doing everything the documentation says to do in this regard, but for widget templates, Django isn’t looking at my project at all, and I’m getting a TemplateDoesNotExist error .

This is everything I covered:

class MyFileWidget(widgets.FileInput):
     template_name = 'myapp/my_file_widget.html'

The template definitely exists. If I pass the template to the render call, it finds it fine. The problem is a path issue. When calling render from the view, it checks for the following:

projectroot/templates/myapp/my_file_widget.html djangoroot/forms/templates/myapp/my_file_widget.html

When it finds the template in my project, it renders it. When I provide the template path in the above class, this does not happen. In this case, it does not check my project template, where the file actually exists, and starts checking the django path instead. Hence the error message.

So I don’t know why the loader is checking my project template on the render call, but not doing so when looking for the “template_name” that the widget overrides. Any ideas?

1> Alasdair..:

By default, the FORM_RENDERER setting defaults to 'django. forms.renderers.DjangoTemplates'.

This checks the application’s template directory (e.g. projectroot/myapp/templates/myapp/my_file_widget.html), but not the project’s template directory (e.g. projectroot/templates/myapp /my_file_widget.html).

If you want to use the same configuration as the TEMPLATES setting, you should use the TemplatesSetting renderer.

FORM_RENDERER = 'django.forms.renderers.TemplatesSetting'

You will also need to update your TEMPLATES settings so that Django can still use the built-in widget templates. The easiest way to do this is to add django.forms code> to INSTALLED_APPS. See the documentation for details on this.

This article is from the internet and does not represent1024programmerPosition, please indicate the source when reprinting:https://www.1024programmer.com/770712

author: admin

Previous article
Next article

Leave a Reply

Your email address will not be published. Required fields are marked *

Contact Us

Contact us


Online consultation: QQ交谈

E-mail: [email protected]

Working hours: Monday to Friday, 9:00-17:30, holidays off

Follow wechat
Scan wechat and follow us

Scan wechat and follow us

Follow Weibo
Back to top