Learnmonkey Learnmonkey Logo
× Warning:

The tutorial you are reading is currently a work in-progress.

Templating

What is Templating?

Templating is returning HTML code when someone visits a route on your site. A template is useful when you want to return an actual HTML site and not just some text. For example, you might have an e-commerce site, but you don't want to just write your HTML code in your Python file. Instead, you can just use an external template and return that instead.

Making Templates in Flask

To make a template in Flask, you will first need to create a folder, named templates. It is very important that the name of the folder is templates. You can write your HTML code in your templates folder. For this tutorial, we will create our Home Page using a template.

First, let's make a file called index.html. We will write HTML code in there. If you are unfamiliar with HTML but want to learn it, then check out our HTML tutorial. But, now, copy the following code into your HTML file:


<!DOCTYPE html>
<html>
  <head>
    <title>Hello</title>
  <head>
  <body>
    <h1>Welcome To My Website!</h1>
    <p>Hello World!</p>
  </body>
</html>
    

If you don't understand the code, you can view the code explanation here.

Displaying Templates

To display templates when we visit a specific route, we use the render_template() function.

Putting Python in Templates