Home > Software design >  Not able to load template that has been extended in Django
Not able to load template that has been extended in Django

Time:09-25

The django 'extends' template is not loading the content. In this I am trying to extend index.html to homepage.html. Both the files are under the same templates directory. The code snippets are shown below:

index.html

<!DOCTYPE html>
{% load static %}
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title> Tashi eComm</title>
    <!-- adding title icon -->
    <link rel = "icon" href ="{% static 'images/TecommLogo.png' %}" type = "image/x-icon">

    <!-- Bootstrap 5 link -->
    <link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-F3w7mX95PdgyTmZZMECAngseQB83DfGTowi0iMjiWaeVhAn4FJkqJByhZMI3AhiU" crossorigin="anonymous">
    <!-- Bootstrap 5 popper and javascript link -->
    <script src="https://cdn.jsdelivr.net/npm/@popperjs/[email protected]/dist/umd/popper.min.js" integrity="sha384-W8fXfP3gkOKtndU4JGtKDvXbO53Wy8SZCQHczT5FMiiqmQfUpWbYdTil/SxwZgAN" crossorigin="anonymous"></script>
    <script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.min.js" integrity="sha384-skAcpIdS7UcVUC05LJ9Dxay8AXcDYfBJqt1CJ85S/CFujBsIzCIv l9liuYLaMQ/" crossorigin="anonymous"></script>
    <!--Bootstrap icons-->
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/[email protected]/font/bootstrap-icons.css">
    <!-- stylesheet -->
    <link rel="stylesheet" type="text/css" href="{% static 'css/stylesheet.css' %}"/>
</head>
<body>
    <div class="wrapper">
        <div class="panel panel-default">
            <div class="panel-heading">
                <div class="d-flex justify-content-end ">
                    <div class="top-header m-2 ">
                        <a href="#">Marketplace</a>
                    </div>
                    <div class="top-header m-2 ">|</div>
                    <div class="top-header m-2">
                        <a href="#">Buyer Protection</a>
                    </div>
                    <div class="top-header m-2 ">|</div>
                    <div class="top-header m-2">
                        <a href="#">Track Order</a>
                    </div>
                    <div class="top-header m-2 ">|</div>
                    <div class="top-header m-2">
                        <div class="form-group">
                            <select id="demo_overview_minimal" class="select-picker" data-role="select-dropdown" data-profile="minimal">
                            <!-- options -->
                                <option>BTN</option>
                                <option>USD</option>
                                <option>EUR</option>
                            </select>
                        </div>
                    </div>

                </div>
            </div>
            <div class="panel-body">

                <nav class="navbar navbar-expand-lg navbar-light bg-light">
                  <div class="container-fluid">
                      <a class="navbar-brand ms-5" href="#"><img src="{% static 'images/TecommLogo.png' %}" alt="Logo" height="60px" width="60px"></a>
                    <button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target="#navbarTogglerDemo01" aria-controls="navbarTogglerDemo01" aria-expanded="false" aria-label="Toggle navigation">
                      <span class="navbar-toggler-icon"></span>
                    </button>
                    <div class="collapse navbar-collapse" id="navbarTogglerDemo01">
                        <ul class="navbar-nav ms-auto mb-2 mb-lg-0">
                            <li class="nav-item dropdown">
                              <a class="nav-link dropdown-toggle" href="#" id="navbarDropdown" role="button" data-bs-toggle="dropdown" aria-expanded="false">
                                Shop by Category
                              </a>
                              <ul class="dropdown-menu" aria-labelledby="navbarDropdown">
                                <li><a class="dropdown-item" href="#">Action</a></li>
                                <li><a class="dropdown-item" href="#">Another action</a></li>
                                <li><hr class="dropdown-divider"></li>
                                <li><a class="dropdown-item" href="#">Something else here</a></li>
                              </ul>
                            </li>
                        </ul>

                        <form class="d-flex Search col-5">
                              <input class="form-control me-auto col-8" type="search" placeholder="Search for anything" aria-label="Search">
                              <button class="btn btn-outline-success" type="submit">Search</button>
                        </form>
                        <ul class="navbar-nav ms-auto mb-2 mb-lg-0">
                        <li class="nav-item">
                          <a class="nav-link active" aria-current="page" href="#">Home</a>
                        </li>
                        <li class="nav-item">
                          <a class="nav-link" href="#">Link</a>
                        </li>
                        <li class="nav-item">
                          <a class="nav-link disabled" href="#" tabindex="-1" aria-disabled="true">Disabled</a>
                        </li>
                      </ul>

                    </div>
                  </div>
                </nav>
            </div>

        </div>

        {% block content %}
        {% endblock content%}

        <footer>
            <div class="panel panel-default">

              <div class="panel-footer bg-light ">
                  <div class="row ms-5 me-5">
                      <div class="col-sm-3">
                          <div class="row">
                              <h3>Policies</h3>
                          </div>
                          <div class="row">
                              <a href="#">Privacy Policies</a>
                          </div>
                          <div class="row">
                              <a href="#">Refund Policies</a>
                          </div>
                          <div class="row">
                              <a href="#">Terms & Conditions</a>
                          </div>
                      </div>
                      <div class="col-sm-3">
                          <div class="row">
                              <h3>Vendor</h3>
                          </div>
                          <div class="row">
                              <a href="#">Start Selling</a>
                          </div>
                          <div class="row">
                              <a href="#">Learn to Sell</a>
                          </div>
                      </div>
                      <div class="col-sm-3">
                          <div class="row">
                              <h3>Connect With Us</h3>
                          </div>
                          <div class="row">
                              <a href="#"><i class="bi bi-facebook"></i>Facebook</a>
                          </div>
                          <div class="row">
                              <a href="#"><i class="bi bi-twitter"></i>Twiiter</a>
                          </div>
                          <div class="row">
                              <a href="#"><i class="bi bi-instagram"></i>Instagram</a>
                          </div>

                      </div>
                      <div class="col-sm-3">
                          <div class="row">
                              <h3>Help & Contact</h3>
                          </div>
                          <div class="row">
                              <a href="#">Contact</a>
                          </div>
                          <div class="row">
                              <a href="#">About Us</a>
                          </div>
                          <div class="row">
                              <a href="#">FAQs</a>
                          </div>
                      </div>
                  </div>
              </div>
            </div>
        </footer>

    </div>

</body>
</html>

homepage.html

{% extends 'index.html' %}

{% block content %}
    Hello! this is my block content
{% endblock content %}

urls.py

from django.conf import settings
from django.conf.urls.static import static
from django.contrib import admin
from django.contrib.staticfiles.urls import staticfiles_urlpatterns
from django.urls import path, include
from . import views

urlpatterns = [
    path('admin/', admin.site.urls),
    path('', views.homepage, name="homepage"),
    # path('accounts/', include('accounts.urls')),
]

urlpatterns  = staticfiles_urlpatterns()

urlpatterns  = static(settings.MEDIA_URL,
                      document_root=settings.MEDIA_ROOT)

views.py

from django.shortcuts import render


def homepage(request):
    return render(request, 'index.html')

I have done the similar projects earlier but it was working fine. I don't know why it doesn't work in this project while i have done everything similar I have been trying this for a whole day. Any help will be appreciated.

CodePudding user response:

You want to extend index.html to homepage.html so you have to call homepage.html in your views.py file.

from django.shortcuts import render


def homepage(request):
    return render(request, 'homepage.html')
  • Related