[PYTHON] Common html to rent with Django

Overview

If you write <header> or <footer>, <link> or <script> that defines the reference destination of js or css in html file units, maintainability will decrease. Is it possible to cite other html files with Layout Dialect like thymeleaf? After investigating, django has a concept of template inheritance, so I will use it.

About Django template (html) standardization and inheritance

Inheritance relationship of html file

Describe the common html tag in the reference source, and insert the template tags `{% block <block name>%} ~ {% endblock%}` in the part that switches depending on the reference source.

parent.html


<!DOCTYPE html>
<html>
    <head>
    </head>
    <body>
{% block mainblock %}
(1)
{% endblock %}
    </body>
</html>

Describe the template tag ``` {% extends" html file name "%} `` in the reference destination, and the template tag {% block <block name>%} ~ {% endblock%} in the content to be switched. Insert `` For the html file name specified in extends, specify the full path from the templete directory created by django.

child.html


<!DOCTYPE html>
{% extends "pull/parent.html" %} (2)

{% block mainblock %}
(3)
<div>
    <span>hello python!!</span>
</div>
{% endblock %}

(1). Area to switch depending on the reference destination. You can specify multiple blocks. (2). Specify the html file to be referenced. (3). Write the area you want to insert in the reference source.

After rendering

<!DOCTYPE html>
<html>
    <head>
    </head>
    <body>
        <div>
            <span>hello python!!</span>
        </div>
    </body>
</html>

Impressions

Recommended Posts

Common html to rent with Django
Convert .ipynb to .html (with BatchFile)
Steps to develop Django with VSCode
How to get started with Django
How to authenticate with Django Part 2
How to authenticate with Django Part 3
[Tips] How to do template extends when creating HTML with django
How to do arithmetic with Django template
Step notes to get started with django
HTML email with image to send with python
Django HTML Template # 2
Internationalization with django
Django HTML template
CRUD with Django
The easiest way to get started with Django
How to implement "named_scope" of RubyOnRails with Django
Authenticate Google with Django
Django 1.11 started with Python3.6
Upload files with Django
Development digest with Django
I tried to create a table only with Django
Output PDF with Django
Markdown output with Django
Steps from installing Django to displaying an html page
Use Gentelella with django
Twitter OAuth with Django
Getting Started with Django 1
When you want to filter with Django REST framework
Send email with Django
File upload with django
unable to import django
Use LESS with Django
Single sign-on to your Django application with AWS SSO
Pooling mechanize with Django
Transit to the update screen with the Django a tag
Things to do when you start developing with Django
Use MySQL with Django
Things to watch out for when migrating with Django
update django version 1.11.1 to 2.2
Model.objects.extra to consider before using raw SQL with Django
Start today with Django
Getting Started with Django 2
It's too easy to use an existing database with Django
How to handle static files when deploying to production with Django
How to check ORM behavior in one file with django
I want to make a blog editor with django admin
[Django] How to give input values in advance with ModelForm
How to resolve CSRF Protection when using AngularJS with Django
I'm trying to create an authentication / authorization process with Django
I can't log in to the admin page with Django3
For beginners, how to deal with common errors in keras
[Django] Redirect to previous page after submitting with POST form
Try to factorial with recursion
Connect to BigQuery with Python
Do Django with CodeStar (Python3.6.8, Django2.2.9)
Get started with Django! ~ Tutorial ⑤ ~
Minimal website environment with django
Create an API with Django
Do Django with CodeStar (Python3.8, Django2.1.15)
Deploy Django serverless with Lambda
Python3 + Django ~ Mac ~ with Apache