71

Can I create a base template for my project which all apps can draw from? Or do I have to create a base template for each app? And if I wanted them to be the same I'd just copy them?

heri0n
  • 1,459
  • 3
  • 19
  • 33

5 Answers5

104

Sure you can. A quick example of a base.html

<!DOCTYPE html>
<html>
    <head>
        <title>My Project</title>
    </head>

    <body>
    {% block content %}{% endblock content %}
    </body>
</html>

And say you have a app called myapp with a view.html page,

{% extends "base.html" %}

{% block content %}
    <h2>Content for My App</h2>
    <p>Stuff etc etc.</p>
{% endblock content %}

Take some time to read the docs for more information

Denis
  • 940
  • 12
  • 16
Kenny Shen
  • 4,773
  • 3
  • 21
  • 18
  • 10
    An important point that many people miss is that `{% extends xxx %}` can take either a string literal file name (i.e. "base.html") *or* a variable which resolves to a file name. We use this during development and change the value of `{% extends base_template %}` on-the-fly. All of the primary templates (lists, detail, flat files, etc.) use very generic / semantic markup, and all the CSS/etc. is defined in the per-theme base file. E.g. `themes/blue.html` or `themes/neon.html`. – Peter Rowell Feb 06 '13 at 03:44
  • 17
    It's not very clear to me *where* these files must go. I want my base template inside PROJECT_ROOT/templates/base.html, so it is available to all apps. But now how can I reference it, given that the 'extend' statement looks for templates inside PROJECT_ROOT/app-name/templates/...? – jlh Feb 13 '15 at 10:56
  • 1
    @jlh: It worked for me when I put my templates in PROJECT_ROOT/project-name/templates/base.html – Andreas Jan 30 '16 at 14:16
  • 1
    @jih You can tell Django to look in `PROJECT_ROOT/some_app/templates` by editing the `settings.py` file - for example: `'DIRS': [os.path.join(BASE_DIR, "some_app", "templates")]` – alex Nov 18 '18 at 18:54
39

For Django version 3

Some of the answers here are correct, but they do not tell you where to place the files in the directory structure. So I am going to explain the answer a bit.



Yes, you can use {% extends "base.html" %} to extend the base.html file from your project directory.
Important thing to note is where to place the base.html file.

  1. open project_name/project_name/settings.py and find the TEMPLATES array
    and update the 'DIRS' : [] to 'DIRS': [os.path.join(BASE_DIR, 'templates')]
  2. Create a directory at root level named templates. The location of this folder will be project_name/templates
  3. Place all templates here in projects_name/templates that you want to access from all the apps.
    File: project_name/templates/base.html
  4. Extend this base file from any application-specific template, that might be located at project_name/app_name/templates/app_name/template_file.html by using {% extends "base.html" %}.

    {% extends "base.html" %}
    
    {% block content %}
        <h2>Content for My App</h2>
        <p>Stuff etc etc.</p>
    {% endblock content %}
    



Additionally, you can also create project-wide static directory at the same level as template directory by updating STATICFILES_DIRS to STATICFILES_DIRS = [os.path.join(BASE_DIR, "static")] in project_name/project_name/settings.py.


Final Directory Structure looks like -

project_name/
    app_name/
        templates/
            app_name/
                template_file.html

    project_name/
        settings.py

    templates/
        base.html

    static/
        css/
        js/
        img/
mukesh.kumar
  • 1,100
  • 16
  • 30
9

There is some problem in last answer and here is the correct one; you must have Base.html like this:

{% load staticfiles %}

<!DOCTYPE html>
<html>
<head>
    <title>My Project</title>
</head>
<body>
{% block content %}{% endblock %}
</body>
</html>

and also for index.html

{% extends "appname/base.html" %}
{% block content %}
     <h1>test</h1>
{% endblock %}
Tryph
  • 5,946
  • 28
  • 49
Mohammad.Gh
  • 385
  • 3
  • 16
7

Yes you can absolutely do that. By using extends and include template tags in your Django templates.

I am starting to learn Django and recently figured this out. My code is at Github if you are interested in taking a look at how I structure Django templates to inherit from a base.html and then include common stuff such as navbar and header, footer etc.

redDragonzz
  • 1,543
  • 2
  • 15
  • 33
0

Yes, you can create a base template for your project that other apps will extend. Check @Kenny_Shen answer on how.

Just wanted to add two notes if you are concerned about app isolation.

  1. If you do not want your app to rely on the "base.html" naming convention, you can inject the actual name with a variable, take a look at: How do I use Django's template extends variable? for details,

  2. You might want to name the app template block to something else, not "content" to avoid collisions with other apps.

gmagno
  • 1,770
  • 1
  • 19
  • 40