[PYTHON] Flask reuse How to write html

As a memorandum.

templates/index.html


{% extends 'base.html' %}
{% block main %}
    <main>
        <p>Main content</p>
        <p><a href="/another">Go to another page</a></p>
    </main>
{% endblock %}

templates/another.html


{% extends 'base.html' %}
{% block main %}
    <main>
        <p>Another content</p>
        <p><a href="/">Back to top page</a></p>
    </main>
{% endblock %}

templates/base.html


<html>
    <head>
        <title>Flask lesson</title>
    </head>
    <body>
    <header>
        <div><h1>HEADER</h1></div>
        <hr>
    </header>    
{% block main %}
{% endblock %}
    <footer>
        <hr>
        <div><h1>FOOTER</h1></div>
    </footer>
    </body>
</html>

run.py


from flask import Flask,render_template
app = Flask(__name__)

@app.route('/')
def index():
    return render_template('index.html')

@app.route('/another')
def second():
    return render_template('another.html')

app.debug =  True
app.run()

--extend means to extend --Specify the extension destination html with {% extends ~~~ .html%} -Enclose the code you want to embed in {% block ~~~%} {% endblock%}, and write it in the location you want to embed in the extension destination --You can also write and embed multiple {% block ~~~%} {% endblock%}

PHP include may be easier to manage.

Recommended Posts

Flask reuse How to write html
How to write a Python class
How to write soberly in pandas
How to write Docker base image
How to write Django1.9 environment-independent wsgi.py
Notes on how to write requirements.txt
[Memo] How to use BeautifulSoup4 (1) Display html
How to set optuna (how to write search space)
How to write Python document comments (Docstrings)
How to write this process in Perl?
How to write Ruby to_s in Python
Summary of how to write AWS Lambda
How to write pydoc and multi-line comments
How to start Python (Flask) when EC2 starts
Answer to "Offline real-time how to write F02 problem"
How to write regular expression patterns in Linux
How to write a ShellScript Bash for statement
Answer to "Offline Real-time How to Write F01 Problem"
How to search HTML data using Beautiful Soup
Answer to "Offline Real-time How to Write E13 Problem"
How to write async and await in Vue.js
How to write a named tuple document in 2020
[Go] How to write or call a function
How to write a ShellScript bash case statement
How to use xml.etree.ElementTree
How to use Python-shell
20th Offline Real-time How to Write Problems in Python
How to use tf.data
How to use virtualenv
Scraping 2 How to scrape
How to use Seaboan
How to use image-match
How to use shogun
How to install Python
How to use Pandas 2
How to read PyPI
How to install pip
How to use numpy.vectorize
How to install archlinux
How to use pytest_report_header
Easy to use Flask
How to upload with Heroku, Flask, Python, Git (Part 3)
How to restart gunicorn
How to install python
How to virtual host
How to write a list / dictionary type of Python3
How to debug selenium
How to use partial
How to use Bio.Phylo
How to read JSON
How to use SymPy
How to use x-means
How to use WikiExtractor.py
How to update Spyder
How to use IPython
How to install BayesOpt
How to use virtualenv
How to use Matplotlib
How to upload with Heroku, Flask, Python, Git (Part 1)
[Python] How to write a docstring that conforms to PEP8
How to use iptables