Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Override Django widgets default templates

I want to override the Django (2.01) widget templates, because I need to add classes for each input, label, and position them differently

app
 - templates
 - app 
    - django
       - forms
           - widgets
             - input.html

enter image description here or

app
 - templates
   - django
      - forms
          - widgets
             - input.html

or template project directory:

- templates
   - django
      - forms
          - widgets
             - input.html

None of them works, (even if I this is how is recommended in docs, and from an answer that I saw on stackoverflow), it still loads from default.

Being general widgets templates, I'm preferring to put them in the template project directory, but from what I reed by default for widgets search only in installed apps.

like image 252
user3541631 Avatar asked Jan 12 '18 12:01

user3541631


People also ask

How do I override a Django template?

To override these templates, you will need to have an admin folder in your templates folder. If you do not have a templates folder, you can create it in the main project folder. To do so, you will have to change the project's settings.py . Find the TEMPLATES section and modify accordingly.

What does {% %} mean in Django?

{% extends variable %} uses the value of variable . If the variable evaluates to a string, Django will use that string as the name of the parent template. If the variable evaluates to a Template object, Django will use that object as the parent template.

Where are Django admin templates stored?

The default templates used by the Django admin are located under the /django/contrib/admin/templates/ directory of your Django installation inside your operating system's or virtual env Python environment (e.g. <virtual_env_directory>/lib/python3. 5/site-packages/django/contrib/admin/templates/ ).

What is App_dirs in Django?

With APP_DIRS set to True , the template loader will look in the app's templates directory and find the templates.


1 Answers

It looks like this was a problem in Django 1.11.

Django widget template override does not search in the project template directory. How to fix?

This is somewhat annoying. It is fixed by changing your settings. So your main site has to have these settings.

INSTALLED_APPS = [
    ...
    'django.forms',
    ...
]

FORM_RENDERER = 'django.forms.renderers.TemplatesSetting'

Unfortunately, you cannot automate this by one of your installed apps. If an installed app overrides django template widgets it needs to be documented and users have to add this code in their site settings.

You don't need to change anything in the "TEMPLATES" setting. Here is what my TEMPLATES looks like for reference.

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',
            ],
        },
    },
]
like image 138
justengel Avatar answered Nov 16 '22 00:11

justengel