SergeyMoroz
SergeyMoroz

Reputation: 137

Cant extend html template on Django

I cant extends one html document (child - base_home.html) to another parent html document - base.html I have next Django project structure:

Testdjango/
blog/
   migrations/
   templates/
       blog/
          base.html
          base_home.html
   __init.py__
   admin.py
   apps.py
   models.py
   tests.py
   urls.py
   views.py
db.sqlite3
manage.py

My parent template is a base.html, with the next code:

   <!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <!-- The above 3 meta tags *must* come first in the head; any other head content must come *after* these tags -->
    <meta name="description" content="">
    <meta name="author" content="">
    <title>{% block title %}{% endblock %} &ndash; Blog</title>

    <!-- Bootstrap core CSS -->
    <link href="https://maxcdn.bootstrapcdn.com/bootswatch/3.3.7/journal/bootstrap.min.css" rel="stylesheet">

    <!-- Custom styles for this template
    <link href="starter-template.css" rel="stylesheet"> -->

    </head>

  <body>

    <nav class="navbar navbar-inverse navbar-fixed-top">
      <div class="container">
        <div class="navbar-header">
          <button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#navbar" aria-expanded="false" aria-controls="navbar">
            <span class="sr-only">Toggle navigation</span>
            <span class="icon-bar"></span>
            <span class="icon-bar"></span>
            <span class="icon-bar"></span>
          </button>
          <a class="navbar-brand" href="#">Blog</a>
        </div>
        <div id="navbar" class="collapse navbar-collapse">
          <ul class="nav navbar-nav">
            <li class="active"><a href="#">Home</a></li>
            <li><a href="#about">About</a></li>
            <li><a href="#contact">Contact</a></li>
          </ul>
        </div><!--/.nav-collapse -->
      </div>
    </nav>

    <div class="container">
        {% block content %}
        {% endblock %}

    </div>
    <!-- Bootstrap core JavaScript
    ================================================== -->
    <!-- Placed at the end of the document so the pages load faster -->
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
       <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
     </body>
</html>

And my "child" base_home.html have next code:

{% extends "blog/base.html" %}
{% block title %}
Home
{% endblock %}
{% block content %}
Lorem bla bla
{% endblock %}

And in page i cant see anything from base_home.html

Upvotes: 1

Views: 2470

Answers (6)

SergeyMoroz
SergeyMoroz

Reputation: 137

I think i find the asnwer. In my view i have return render(request, "blog/base.html"). I chage it to return render(request, "blog/base_home.html") - where i have extend. And now its working. Tell me please, it true?

Upvotes: 0

Dmitrij
Dmitrij

Reputation: 116

do you have something similar to

TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'APP_DIRS': True,
        'DIRS': [
            str(ROOT_DIR.path('templates')),
        ],`...

in your settings?

and do you have your blog app registered in project settings INSTALLED_APPS?

Upvotes: 0

All Іѕ Vаиітy
All Іѕ Vаиітy

Reputation: 26392

Your templates seems okay, make sure your Template settings looks something like this, also you have added the app to INSTALLED_APPS.

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',
            ],
        },
    },
]

and since base.html and base_home.html are in same dir, extend base as,

{% extends "base.html" %}

Upvotes: 4

Cadmus
Cadmus

Reputation: 665

You can extends the templates in two ways

  1. include ## Include a perticular section of html into template
  2. extends ## Extends common html into diff pages

base.html

<html>
<head></head>
<body>
======
======
</body>
</html>

home.html

If base.html in any folder please mention folder name dont put slash(/) before the foldername `{% extends 'foldername/base.html' %}` 

{% extends 'base.html' %} ## you can see the base.html styles in the home.html

suppose you need to include a perticular div,tables or section of code use {% include 'base.html' %} if its in folder {% include 'foldername/base.html' %}

Upvotes: 0

Bharat Godam
Bharat Godam

Reputation: 477

try

{% extends "base.html" %}

instead of

{% extends "blog/base.html" %}

Upvotes: 0

Dennis Slimmers
Dennis Slimmers

Reputation: 89

Django doesn't recognize which block you are trying to end. You need to define this.

{% extends "blog/base.html" %}
{% block title %}
Home
{% endblock title %}
{% block content %}
Lorem bla bla
{% endblock content %}

Upvotes: -1

Related Questions