[Fixed]-Django: reusable templates: extend from base.html

1πŸ‘

The easy way to solve this problem is to namespace your templates. Create an application and inside the application directory (where you have the default views.py) create a templates directory, and inside that directory create a subdirectory which is the name of the application.

Imagine you have a project myproj and an app called registration, then you would have:

.
β”œβ”€β”€ manage.py
β”œβ”€β”€ myproj
β”‚Β Β  β”œβ”€β”€ __init__.py
β”‚Β Β  β”œβ”€β”€ settings.py
β”‚Β Β  β”œβ”€β”€ urls.py
β”‚Β Β  └── wsgi.py
└── registration
    β”œβ”€β”€ admin.py
    β”œβ”€β”€ __init__.py
    β”œβ”€β”€ migrations
    β”‚Β Β  └── __init__.py
    β”œβ”€β”€ models.py
    β”œβ”€β”€ templates
    β”‚Β Β  └── registration
    β”‚Β Β      └── base.html
    β”œβ”€β”€ tests.py
    └── views.py

Now even if you have another application with a template called base.html, you can always load the specific template you need with {% extends 'registration/base.html' %}

πŸ‘€Burhan Khalid

Leave a comment