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 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.
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.
As I made in the previous article, the location of the file is as follows.
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.
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.
Recommended Posts