Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Base template for all apps in Django

I have a project with 2 apps

project/     blog/         templates/             index.html     polls/         templates/             index.html     project/         templates/             base.html             index.html 

Now i want that the two apps extends the projects base.html. Is this the way to go? how it is possible and are there better solutions?

There is already an question which is handling this question, but it's only worth mentioning if you dont use split directories: Django project base template

tl;dr: I want to use split directories and want to know how to extend a base template from multiple apps without copying it to every app

like image 505
fechnert Avatar asked Apr 07 '15 13:04

fechnert


People also ask

How do I organize my Django apps?

The way I like to organize my Django Project is – Keeps all Django apps in apps folder, static files (scripts, js, CSS) in the static folder, HTML files in templates folder and images and media content in the media folder.

How do I get templates in Django?

To configure the Django template system, go to the settings.py file and update the DIRS to the path of the templates folder. Generally, the templates folder is created and kept in the sample directory where manage.py lives. This templates folder contains all the templates you will create in different Django Apps.

How many apps can a Django project have?

Django comes with six built-in apps that we can examine.

What template does Django use?

The Django template language is Django's own template system. Until Django 1.8 it was the only built-in option available. It's a good template library even though it's fairly opinionated and sports a few idiosyncrasies.


1 Answers

In current Django (1.10) TEMPLATE_DIRS is deprecated, so:

  1. Add a templates directory at the project root,
  2. In settings.py find TEMPLATES -> DIRS and add it like this:

    TEMPLATES = [ {     ...     'DIRS': [(os.path.join(BASE_DIR, 'templates')),],     ... } 
  3. Add a base.html to that directory.

  4. Extend it wherever you want using {% extends 'base.html' %}
like image 126
Oliver Sosa Avatar answered Oct 02 '22 14:10

Oliver Sosa