Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Render multiple templates at once in Flask

I'm making a Flask application. I have a login area, a blogs area. If I want to get user's login, I'll render login template. But this doesn't render the blog template that has to be displayed below the login area. :/

I'll try to make it clearer:

{% block login %} {% endblock %}
blah blah
{% block blog_display %} {% endblock %}

Now i have a login.html which extends this, and goes into login block. I have a blogs.html which goes into blog_display. How to I render both? When I do render_template(), i can call it on only one of the login.html or blogs.html.

Please help me out. I'll give more details if you ask for it.

like image 224
SiddharthaRT Avatar asked Jun 08 '12 10:06

SiddharthaRT


People also ask

How do I render two templates in Flask?

Instead of returning hardcode HTML from the function, a HTML file can be rendered by the render_template() function. Flask will try to find the HTML file in the templates folder, in the same folder in which this script is present.

How do you add separate templates to a different page in Flask?

Simply store all your main code, back end code in app.py. Then all the layout arrangement, template code is in templates. Then inside the templates folder store the html files.

How do I create a render template in Flask?

html template file in a directory called templates inside your flask_app directory. Flask looks for templates in the templates directory, which is called templates , so the name is important. Make sure you're inside the flask_app directory and run the following command to create the templates directory: mkdir templates.

How does render_template work in Flask?

render_template is a Flask function from the flask. templating package. render_template is used to generate output from a template file based on the Jinja2 engine that is found in the application's templates folder. Note that render_template is typically imported directly from the flask package instead of from flask.


2 Answers

You may be thinking about layouts the wrong way. Your layout is the most generic of your templates, not your most complex one. If you need little self-contained pieces of functionality then write them up just as they are and include them where they are needed.

That is to say, if you want something like this:

----------------------------------
                  +--------------+
  Header          |   Login      |
                  +--------------+
----------------------------------

  Body Content (Blog)

And you also want to have a stand-alone login page like this:

----------------------------------

  Header

----------------------------------

  +--------------+
  |   Login      |
  +--------------+

Then create a login partial and include it where you need it.

Example

templates/partials/login.html

<form action="/login" method="post">
<!-- Your login goes here -->
</form>

templates/your_base.html

<!DOCTYPE html>
<html>
<head>
{% block head %}
{# 
Default HEAD content goes here 
with extra nested blocks for children to override 
if needed. 
#}
{% endblock head %}
</head>
<body>
<header>{% block header %}{% endblock header %}</header>
{# Note: This assumes we *always* want a header #}
{% block content %}{% endblock content %}
</body>
</html>

templates/login.html

{% extends "your_base.html" -%}
{% block content -%}
{% include "partials/login.html" %}
{%- endblock content %}

templates/blog.html

{% extends "your_base.html" -%}
{% block header -%}
{{ super() }}{# Render the default header contents here #}
{% include "partials/login.html" %}
{%- endblock header %}
{% block content -%}
{# Render your blog posts here #}
{%- endblock content %}
like image 112
Sean Vieira Avatar answered Sep 19 '22 14:09

Sean Vieira


Sean's answer works good, but if you don't want to extend blocks you can pick up a simpler solution, which i prefer more.

{% include "partials/login.html" %}

Just use it anywhere you need to include the template

like image 38
Eugene Sue Avatar answered Sep 21 '22 14:09

Eugene Sue