Flask Tutorial
About Lesson

In this Flask Tutorial we are going to learn about Flask Template Inheritance, in the previous lesson we have learned that how you can create templates in Flask, now it is time to talk about template inheritance in flask. as we have created three html files in our previous lesson, but the problem is this that we are duplicating our code in the html files, for example in three html files we have header, body and html tags, so we need to remove this duplication from our templates, by this reason we are using Template Inheritance in Flask, by using Flask Template inheritance we are avoiding code duplication in our templates, so now let’s start our coding.



We are not going to bring changes in our app.py file, and this is the app.py file.




We need to create a new html file at name base.html file, this will be a base template for all of our html files, and our all html files will extends from this base.html file, in our base.html, you can see that we have created some blocks for our title and also body, now you can change this block in your other html files. you can create as much blocks as you want according to your requirements.



And in here we have created our index.html file, the first thing you need to extends from the base.html, and after that you need to create the same block names as we have created in our base.html.












Run your Flask application and go to http://localhost:5000/, this is the result.

Flask Tutorial - Flask Template Inheritance
Flask Tutorial – Flask Template Inheritance


Thank you for visiting my website! If you enjoyed the free courses, please consider supporting my works on Patreon.
Exercise Files
No Attachment Found
No Attachment Found