Creating HTML Templates with Flask: Tutorial #2

Posted by


Flask is a lightweight Python web framework that allows you to effortlessly create web applications. In this tutorial, we will be focusing on how to use HTML templates in Flask to create dynamic web pages.

HTML templates in Flask allow you to separate the design of your web pages from the logic of your web application. This makes it easier to manage and maintain your code, as well as create a consistent look and feel across your entire application.

To get started with this tutorial, make sure you have Flask installed on your computer. You can install Flask using pip by running the following command:

pip install Flask

Once you have Flask installed, create a new Python file and import the necessary modules:

from flask import Flask, render_template

Next, create a new Flask app instance:

app = Flask(__name__)

Now, let’s create an HTML template for our web page. Create a new folder in your project directory called "templates". Inside this folder, create a new HTML file called "index.html" with the following content:

<!DOCTYPE html>
<html>
<head>
    <title>Welcome to My Flask App</title>
</head>
<body>
    <h1>Welcome to My Flask App</h1>
    <p>{{ message }}</p>
</body>
</html>

In this HTML template, we have a simple welcome message that will be passed from our Flask application. The message will be dynamic and can be changed based on the logic of our application.

Next, let’s create a route in our Flask application that will render this HTML template. Add the following code to your Python file:

@app.route('/')
def index():
    message = 'Hello, World!'
    return render_template('index.html', message=message)

In this code snippet, we define a route ‘/’ which will render our index.html template. We pass a message variable to our template, which will be displayed on the web page. The message variable contains the text ‘Hello, World!’.

Now, run your Flask application by adding the following code to the end of your Python file:

if __name__ == '__main__':
    app.run()

Save your Python file and run it using the following command in your terminal:

python your_file_name.py

Open your web browser and navigate to http://127.0.0.1:5000/. You should see the welcome message "Welcome to My Flask App" along with the dynamic message "Hello, World!" displayed on the web page.

Congratulations! You have successfully created a Flask application that uses HTML templates to render dynamic web pages. You can now further customize your web pages by adding more HTML templates and integrating them with your Flask application logic. Flask’s HTML templates make it easy to create visually appealing and dynamic web applications with minimal effort.

0 0 votes
Article Rating

Leave a Reply

31 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
@loganboyd
2 hours ago

Funny how much this reminds me of classic ASP and VBScript back in the late '90s

@SonitAmbashta
2 hours ago

For those confused between adding the doctype tag, the <!DOCTYPE html> tag always goes into HTML5 files. Otherwise it will just be the older version of html

@realrishiiscoolgaming9548
2 hours ago

7:10 the mistake i made is i for got to keep the <> in /home lol

@Turbopasta
2 hours ago

to anyone getting not found error around 7:00 mark, make sure the website is going to /somewhere, the default page python launches produces an error with the current code otherwise.

@tash726
2 hours ago

internal server error fix:

rename template to templates

@derenece1708
2 hours ago

Thank you for teaching me so clearly and quickly something I couldn't learn at school.

@marcin_lewandowski
2 hours ago

Thanks Tim

@mohiitsihag
2 hours ago

hey tim, thanks for explaining this in such an easy way , "l will give you few more examples so that you guys really understand it " takes my heart

@DD-mr2tk
2 hours ago

Awesome

@chtholly1625
2 hours ago

.

@andrewzolensky9807
2 hours ago

Hi Tim, thank you for the tutorial. The code for redirecting the admin page is not working for me. Is this code posted anywhere so i can download and try it?

@yogharajar8211
2 hours ago

How to use buttons on rendered template html files. I am unable to do that

@gavinpayne4882
2 hours ago

would {% %} still work if you were doing it from a separate js file linked to the .html?

@angel-dine
2 hours ago

If your render_template is not working, maybe giving you template not found error, make sure the folder which has your index.html file is named "templates" and not "template". Hope this helps.

@krystianmasiak6212
2 hours ago

Tim made so many proofs that I'm 100% sure that he hasn't made this up. 🙂

Awesome video! 🙂

@xtractztv8628
2 hours ago

FYI for future users: I have copied this code on multiple OS systems (Linux, Windows, and Mac) to no avail. The first episode worked just fine after I tweaked it massively; This episode, I copied everything the same. Does not work. "Internal Service Errors" when trying to use a template. I have tried everything, time to find a youtuber that has posted recently on this topic and is using Python 310

@collins797
2 hours ago

Thanks tim. Just heard "Django" instead of flask 8:14. I'm going to have to learn that also, when does it end.

@odinsrensen7460
2 hours ago

Thank you so much. Everything you're saying makes so much more sense and is so much more informative than the other tutorials I've found.

@seankeyes5687
2 hours ago

nothing seems to work the same for me when i watch your flask videos

@matthieukaczmarek3140
2 hours ago

is it possible to have:

<p><% x for x in content %><p>

31
0
Would love your thoughts, please comment.x
()
x