How to extends template in django

Shop Forums Django Discussion How to extends template in django

  • This topic is empty.
Viewing 0 reply threads
  • Author
    Posts
    • #7155
      Projects Plaza
      Participant

      Here we will learn how to extends the template in Django. Suppose we have a home and we want to create about us page with the same header and same footer. We will create base.html and we will extend this template on the home page and about page.

      Views.py

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

      Urls.py

      urlpatterns=[
          path('',views.index,name='index'),
          path('about/',views.about,name='about'),
      ]

      templates/base.html

      <!DOCTYPE html>
      <html lang="en">
      <head>
         <meta charset="UTF-8">
         <meta name="viewport" content="width=device-width, initial-scale=1.0">
         <title>Home Page</title>
      </head>
      <body>
      {% block content %}
      {% blockend %}
      </body>
      </html>

      templates/index.html

      {% extends "base.html" %}
      {% block content %}
      <h3>Home Page</h3>
      {% blockend %}

      templates/about.html

      {% extends "base.html" %}
      {% block content %}
      <h3>About Us Page</h3>
      {% blockend %}
      
      
Viewing 0 reply threads
  • You must be logged in to reply to this topic.