{"id":287,"date":"2024-10-22T13:00:06","date_gmt":"2024-10-22T13:00:06","guid":{"rendered":"https:\/\/kerrprogroup.com\/?p=287"},"modified":"2025-03-19T12:18:19","modified_gmt":"2025-03-19T12:18:19","slug":"how-to-render-templates-in-flask","status":"publish","type":"post","link":"https:\/\/kerrprogroup.com\/index.php\/2024\/10\/22\/how-to-render-templates-in-flask\/","title":{"rendered":"How to Render Templates in Flask"},"content":{"rendered":"
Flask<\/strong> is a lightweight web framework for Python that makes it easy to build web applications. In our previous article, we\u2019ve seen how to set up a simple page<\/a>.<\/p>\n But, it\u2019s just a simple text like \u201cHello Flask!\u201d<\/strong>. In real applications, you\u2019ll want to render more than just simple text. You\u2019ll want to render HTML templates.<\/p>\n In Flask, we can do so with Jinja<\/a><\/strong>.<\/p>\n One of its powerful features is the ability to render HTML templates using the Jinja templating engine.<\/p>\n Jinja templates are written in regular HTML that you are already familiar with. But, on top of that, you can use special syntax to handle dynamic content, such as for passing data from requests or other code sources to the template. It also allows you to use control structures like loops and conditionals.<\/p>\n Let\u2019s see how it works in its basic form:<\/p>\n Welcome back, {{ user.name }}!<\/p>\r\n {% else %}\r\n Please log in to continue.<\/p>\r\n {% endif %}\r\n\r\n\r\n<\/pre>\n It looks like a regular HTML file, but as we can see, we use some special syntax, such as the To render the template, we need to put it in the Then, we use the In our template above, we have To pass variables to the template, we can do so by passing them as arguments to the For example:<\/p>\n Now, when we visit the home page, we will see the \u201cWelcome back, Joann!\u201d<\/strong> message instead of \u201cPlease log in to continue.\u201d<\/strong> because the Jinja allows you to use template inheritance, which is helpful when multiple pages share the same layout or components. Instead of repeating common elements, we can create a base template and reuse it in other templates.<\/p>\n In this example, we have a homepage and an about page that share the same layout, like the First, we create the base file. Let\u2019s name it In this template, there are two blocks: one for the title and one for the content. The Now, we can rewrite the Welcome back, {{ user.name }}!<\/p>\n{% else %}\n Please log in to continue.<\/p>\n{% endif %}\n{% endblock %}\r\n<\/pre>\n On the about page, we could also rewrite it as follows:<\/p>\n This is the about page.<\/p>\r\n{% endblock %}\r\n<\/pre>\n When we visit the homepage, we\u2019ll see the \u201cWelcome back, Joann!\u201d<\/strong> message if the user is logged in. On the about page, we\u2019ll see \u201cThis is the about page.\u201d<\/strong>. The difference here is that both pages are cleaner and easier to maintain, without repeating code.<\/p>\n That\u2019s how we can render HTML templates in Flask using Jinja. Jinja is a powerful templating engine that comes with helpful features and extensions<\/a>, making it easier to build dynamic web applications in Python.<\/p>\n There\u2019s still a lot more to explore, but I hope this article gives you a solid starting point to get up and running with Flask.<\/p>\n The post How to Render Templates in Flask<\/a> appeared first on Hongkiat<\/a>.<\/p>\n","protected":false},"excerpt":{"rendered":" Flask is a lightweight web framework for Python that makes it easy to build web applications. In our previous article, we\u2019ve seen how to set up a simple page. But, it\u2019s just a simple text like \u201cHello Flask!\u201d. In real applications, you\u2019ll want to render more than just simple text. You\u2019ll want to render HTML templates. In Flask, we can do so with Jinja. Jinja One of its powerful features…<\/p>\n","protected":false},"author":1,"featured_media":289,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[11],"tags":[],"_links":{"self":[{"href":"https:\/\/kerrprogroup.com\/index.php\/wp-json\/wp\/v2\/posts\/287"}],"collection":[{"href":"https:\/\/kerrprogroup.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/kerrprogroup.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/kerrprogroup.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/kerrprogroup.com\/index.php\/wp-json\/wp\/v2\/comments?post=287"}],"version-history":[{"count":2,"href":"https:\/\/kerrprogroup.com\/index.php\/wp-json\/wp\/v2\/posts\/287\/revisions"}],"predecessor-version":[{"id":290,"href":"https:\/\/kerrprogroup.com\/index.php\/wp-json\/wp\/v2\/posts\/287\/revisions\/290"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/kerrprogroup.com\/index.php\/wp-json\/wp\/v2\/media\/289"}],"wp:attachment":[{"href":"https:\/\/kerrprogroup.com\/index.php\/wp-json\/wp\/v2\/media?parent=287"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/kerrprogroup.com\/index.php\/wp-json\/wp\/v2\/categories?post=287"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/kerrprogroup.com\/index.php\/wp-json\/wp\/v2\/tags?post=287"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}} <\/figure>\n
Jinja<\/h4>\n
\r\n\r\n\r\n\r\n
{{ }}<\/code> to insert a variable, and we use
{% %}<\/code> for adding loops and conditionals.<\/p>\n
Rendering the Templates<\/h4>\n
templates<\/code> directory.<\/p>\n
\r\n.\r\n|-- app.py\r\n|-- templates\r\n |-- about.html\r\n |-- index.html\r\n<\/pre>\n
render_template<\/code> function in our Python app file to render HTML templates. If we extend the same code from our previous article<\/a>, our code would now look like the following:<\/p>\n
\r\nfrom flask import Flask, render_template\r\n\r\napp = Flask(__name__)\r\n\r\n@app.route('\/')\r\ndef home():\r\n return render_template('index.html')\r\n\r\n@app.route('\/about')\r\ndef about():\r\n return render_template('about.html')\r\n<\/pre>\n
Passing Variables<\/h5>\n
{{ name }}<\/code> and
{{ user.is_logged_in }}<\/code> variables. But, we haven\u2019t passed any variables to the template yet.<\/p>\n
render_template<\/code> function.<\/p>\n
\r\n@app.route('\/')\r\ndef home():\r\n return render_template('index.html', user={'is_logged_in': True, 'name': 'Joann'})\r\n<\/pre>\n
is_logged_in<\/code> is set to
True<\/code>.<\/p>\n
Template Inheritance<\/h5>\n
head<\/code>,
title<\/code>, and maybe some styles or scripts later on. With template inheritance, we can make things simpler and more organized.<\/p>\n
base.html<\/code>. Then, add the common elements:<\/p>\n
\r\n\r\n\r\n\r\n
{% block %}<\/code> tag allows child templates to override specific sections of the base template.<\/p>\n
index.html<\/code> file to extend the
base.html<\/code> file. We can remove all of the basic HTML structure, like the
head<\/code> and
title<\/code> tags from the
index.html<\/code> file, and replace it with the
extends<\/code> tag.<\/p>\n
\r\n{% extends 'base.html' %}\n\n{% block title %}Welcome to Flask{% endblock %}\n\n{% block content %}\n{% if user.is_logged_in %}\n
\r\n{% extends 'base.html' %}\r\n\r\n{% block title %}About Us{% endblock %}\r\n\r\n{% block content %}\r\n
Wrapping Up<\/h4>\n