Introduction
This article is a part of a 30 day Python challenge series. You can find the links to all the previous posts of this series here:
- 30 Days of Python 👨💻 - Day One - Introduction
- 30 Days Of Python 👨💻 - Day 2 - Data Types I
- 30 Days of Python 👨💻 - Day 3 - Data Types II
- 30 Days Of Python 👨💻 - Day 4 - Data Types III
- 30 Days Of Python 👨💻 - Day 5 - Conditions And Loops I
- 30 Days Of Python 👨💻 - Day 6 - Loops II And Functions
- 30 Days Of Python 👨💻 - Day 7 - Developer Environment
- 30 Days Of Python 👨💻 - Day 8 - OOP Basics
- 30 Days Of Python 👨💻 - Day 9 - OOP Pillars
- 30 Days Of Python 👨💻 - Day 10 - OOP Missing Pieces
- 30 Days Of Python 👨💻 - Day 11 - Functional Programming
- 30 Days Of Python 👨💻 - Day 12 - Lambda Expression And Comprehensions
- 30 Days Of Python 👨💻 - Day 13 - Decorators
- 30 Days Of Python 👨💻 - Day 14 - Handling Errors
- 30 Days Of Python 👨💻 - Day 15 - Generators
- 30 Days Of Python 👨💻 - Day 16 - Module Basics
- 30 Days Of Python 👨💻 - Day 17 - External Modules
- 30 Days of Python 👨💻 - Day 18 - File I/O
- 30 Days Of Python 👨💻 - Day 19 - Regular Expressions
- 30 Days Of Python 👨💻 - Day 20 - Debugging And Testing
- 30 Days Of Python 👨💻 - Day 21 - Scripting Basics
- 30 Days of Python 👨💻 - Day 22 - Scripting Extras
- 30 Days Of Python 👨💻 - Day 23 - Web Scraping
- 30 Days Of Python 👨💻 - Day 24 - Web Development Basics
Starting from the basic skeleton of the Flask app, today I implemented some basic features to complete our simple minimalist blog application. First using Flask’s template inheritance pattern, I worked on re-using common template code and then added two dummy posts to render them dynamically. Finally, generating a requirements.txt file to store all the package dependencies in a single file which can then be used to download all the packages using a single command.
Template Inheritance
Currently, the index.html, about.html templates contain duplicate HTML code. Since Flask uses Jinja as the templating engine, we can use the powerful template inheritance concept to create a base template file which would contain all the common HTML code such as the HTML skeleton, navbar, footer, etc. Other templates can then extend this base template hence making our template code more reusable. I created a base template file layout.html to contain the common template structure as follows:
layout.html
- {% extends "layout.html" %}
- {% block title %}Index{% endblock %}
- {% block head %}
- {{ super() }}
- <style type="text/css">
- .important { color: #336699; }
- </style>
- {% endblock %}
- {% block content %}
- <h1>About</h1>
- <p class="important">
- Hi. I am Arindam. I love building User Interfaces. I am currently learning
- Python and created this simple blog using the Flask web development
- framework.
- {% endblock %}
A template re-usable block can be created and the code for that block needs to be placed within {% block block_name %} {% endblock %} The {% block %} informs the template engine that a child template may override these portions of the template.
The other files can then inherit this base template like this:
about.html

Join the conversation! Your thoughts help the community grow.