A super introduction to Django by Python beginners! Part 3 I tried using the template file inheritance function

About this article

This is the output page of the result of learning about Django on Udemy. This is a continuation of the previous article . This time, I'll try using Django's template file inheritance feature.

urls.py This time we will explain the inheritance function of the template file, so ulrs.py is the same as last time.

first\myapp\urls.py


from django.urls import path
from . import views

app_name = 'myapp'

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

views.py views.py is the same as last time, but the code is below just in case.

first\myapp\views.py


from django.shortcuts import render

def index(request):
    context = {
        'names':['Suzuki','Sato','Takahashi'],
        'message':'Hello.',
    }
    return render(request, 'myapp/index.html', context)

Create a base HTML file

Create the HTML that will be the inheritance source. The location will be the folder one level above index.html that was created last time. Please note that they are not in the same folder. image.png

Once created, enter the code below.

first\myapp\templates\base.html


<html>
    <head>
        {% block title %}
            <title>Inheritance source page</title>
        {% endblock %}
    </head>
    <body>
        <h1>Django study page</h1>
        <div>
            {% block body %}
            {% endblock %}
        </div>
    </body>
</html>

There are two points. Enter {% block title%} {% endblock%} in the head part, Enter {% block body%} {% endblock%} in the body part. Next, let's edit index.html which is the inheritance destination.

Editing the inherited template file (index.html)

As I made in the previous article, the location of the file is as follows. image.png

Open the file and write as follows.

first\myapp\templates\myapp\index.html


{% extends "base.html" %}

{% block title %}
    <title>Django study page</title>
{% endblock %}

{% block body %}
    <p>{{ names.0 }}Mr.{{ message }}</p>
    <p>{{ names.1 }}Mr.{{ message }}</p>
    <p>{{ names.2 }}Mr.{{ message }}</p>

    <hr>

    {% for name in names %}
        <p>{{ name }}Mr.{{ message }}</p>
    {% endfor %}
{% endblock %}

A description of the code. First, inherit the template file created earlier with {% extends "base.html"%}. This is a case where a common mistake is to put base.html in the wrong place and get an error.

Next, I put the title tag inside {% block title%} {% endblock%}. This means putting in a unique title for the index.html page. This is useful when you want to create an html page other than index.html and give it a different title. If you do not enter anything, the title in base.html will be reflected and "Inheritance source page" will be displayed.

Next, enclose the entire body part created last time with {% block body%} {% endblock%}. This is also the same as title. index.html It means to describe the body unique to the page.

Besides that, I often do things like inheriting the navigation part and inheriting the footer part. By using the inheritance function, even if there is a change in a part of the page, it will be easier to reflect it in the whole. Django has a lot of features, but I think the inheritance feature is something you definitely use.

Operation check

Start the development server with py manage.py runserver and check the operation. It says "This is a Django study page" that I filled out only in base.html. The page title is displayed as "Django Study Page" If the body part can be displayed normally, it is successful. image.png

Recommended Posts

A super introduction to Django by Python beginners! Part 3 I tried using the template file inheritance function
A super introduction to Django by Python beginners! Part 2 I tried using the convenient functions of the template
A super introduction to Django by Python beginners! Part 6 I tried to implement the login function
A super introduction to Django by Python beginners! Part 1 I tried to display an HTML page that only says "Hello World"
A super introduction to Django by Python beginners! Part 5 I made a super simple diary application with a class-based general-purpose view
A super introduction to Django by Python beginners! Part 4 I made a super-simple diary application (created only with functions without using a class-based general-purpose view)
I tried to analyze the New Year's card by myself using python
I tried reading a CSV file using Python
I tried using the Datetime module by Python
[Python] Smasher tried to make the video loading process a function using a generator
[Python] I tried to get the type name as a string from the type function
I tried to approximate the sin function using chainer
Introduction to AI creation with Python! Part 2 I tried to predict the house price in Boston with a neural network
[Python] You can save an object to a file by using the pickle module.
I tried to make a function to retrieve data from database column by column using sql with sqlite3 of python [sqlite3, sql, pandas]
[Django] I tried to implement access control by class inheritance.
I tried to implement the mail sending function in Python
I tried to make a stopwatch using tkinter in python
I tried to divide the file into folders with Python
I tried to approximate the sin function using chainer (re-challenge)
I tried to create a RESTful API by connecting the explosive Python framework FastAPI to MySQL.
I also tried to imitate the function monad and State monad with a generator in Python
I tried to solve the ant book beginner's edition with python
I tried to convert a Python file to EXE (Recursion error supported)
I tried to get the index of the list using the enumerate function
[Introduction to Python] How to split a character string with the split function
I tried to make a regular expression of "amount" using Python
[Introduction to simulation] I tried playing by simulating corona infection ♬ Part 2
I tried to make a regular expression of "time" using Python
I tried to make a regular expression of "date" using Python
[Introduction] I want to make a Mastodon Bot with Python! 【Beginners】
I tried to make a todo application using bottle with python
[Python] I tried to make a simple program that works on the command line using argparse.
A super introduction to Python bit operations
I tried to refer to the fun rock-paper-scissors poi for beginners with Python
I tried to communicate with a remote server by Socket communication with Python.
Python beginners publish web applications using machine learning [Part 2] Introduction to explosive Python !!
I tried to verify and analyze the acceleration of Python by Cython
[Introduction to Python] How to write a character string with the format function
I made a program to check the size of a file in Python
I tried to create a sample to access Salesforce using Python and Bottle
I tried to verify the result of A / B test by chi-square test
Python: I want to measure the processing time of a function neatly
I made a function to see the movement of a two-dimensional array (Python)
I tried to open the latest data of the Excel file managed by date in the folder with Python
I tried to refactor the template code posted in "Getting images from Flickr API with Python" (Part 2)
[Python] I tried substituting the function name for the function name
vprof --I tried using the profiler for Python
<Python> A quiz to batch convert file names separated by a specific character string as part of the file name
[Python] I tried to solve 100 past questions that beginners and intermediates should solve [Part 5/22]
[Circuit x Python] How to find the transfer function of a circuit using Lcapy
[Python] Deep Learning: I tried to implement deep learning (DBN, SDA) without using a library.
I tried using the Python library "pykakasi" that can convert kanji to romaji.
[Python] I tried to solve 100 past questions that beginners and intermediates should solve [Part 4/22]
[Python] I tried to solve 100 past questions that beginners and intermediates should solve [Part3 / 22]
[Python] I tried to solve 100 past questions that beginners and intermediates should solve [Part 1/22]
Introduction to AI creation with Python! Part 3 I tried to classify and predict images with a convolutional neural network (CNN)
Run a Python file from html using Django
I tried to understand the learning function of neural networks carefully without using a machine learning library (first half).
I want to get the file name, line number, and function name in Python 3.4
I made a script to record the active window using win32gui of Python