how to pass multiple templates to flask.render_template()

  • Last Update :
  • Techknowledgy :

As far as i see you are trying to render static header and footer. I'd recommend to prepare something like "layout.html" with included header and footer:

    <header>//your static header</header>
        {% block body %}
        //content will be here
        {% endblock %}
    <footer> //your static footer </footer>

then in "child" templates(ex: index.html)use:

   % extends "layout.html" %
} {
   % block body %
//your code here
   % endblock %

Exactly like @jbasko wrote! Making use of two {{block}} statements worked for me: For example, I put in my

def page1():
   return render_template('page1.html', title = 'Title')

the page1.html is containing simply two block-specifications

{% extends "page1.html" %}
{% block content %}  
<h1> Content </h1>
{% endblock content %}

{% block info %}
<h1> Info </h1>
{% endblock info %}

which gets referenced in layout.html in two separate places:

    % block content %
 } {
    % endblock %

Suggestion : 2

Last Updated : 13 May, 2022

Setting up the Virtual Environment: To set up a virtual environment, we can make use of Python Package manager “pip” to install the “virtualenv” package. 

pip install virtualenv

Creating Virtual Environment: After the package has been installed we need to create a virtual environment in our project folder. So you can locate an empty folder where you want to create the Flask application or create an empty folder in your desired path. To create the environment we simply use the following command.

virtualenv venv

For Windows:

venv\ Scripts\ activate

Installing Flask: After the virtual environment has been set up, we can simply install flask with the following command:

pip install flask

To run the server, enter the command :

flask run
<!DOCTYPE html>


   <h2>Welcome To GFG</h2>
   <h4>Flask: Rendering Templates</h4>
   <a href="/home">Home</a>
   <a href="/">Index</a>
   <a href="/about">About</a>
   <a href="/documentation">Documentation</a>

   <p> This is a home page</p>
   <p>must use extends not include</p>


Suggestion : 3

How do i pass multiple variables from the database: eg: in my Mongo database: i have these things in my database and i would like to pass them all to my template.,I am using Flask(as framework) and MongoDB(as database server). Right now, all i can do is just pass one argument that i got from the database:,You can pass multiple parameters to the view.

@app.route('/im/', methods = ['GET', 'POST'])
def im_research(user = None):
   error = None
if request.method == 'POST':
   if request.form['user']:
   user = mongo.db.Users.find_one_or_404({
      'ticker': request.form['user']
return redirect(url_for('im_user', user = user))
   flash('Enter a different user')
return redirect(url_for('im'))
if request.method == 'GET':
   return render_template('im.html', user = None)
   users: 'xxx'
   content: 'xxx'
   timestamp: 'xxx'
def index():
   content = ""
user = "Hero"
return render_template('index.html', ** locals())
def index():
   return render_template('index.html', obj = "object", data = "a223jsd");

Suggestion : 4

You’ve written the authentication views for your application, but if you’re running the server and try to go to any of the URLs, you’ll see a TemplateNotFound error. That’s because the views are calling render_template(), but you haven’t written the templates yet. The template files will be stored in the templates directory inside the flaskr package.,Each page in the application will have the same basic layout around a different body. Instead of writing the entire HTML structure in each template, each template will extend a base template and override specific sections.,The base template is directly in the templates directory. To keep the others organized, the templates for a blueprint will be placed in a directory with the same name as the blueprint.,Templates are files that contain static data as well as placeholders for dynamic data. A template is rendered with specific data to produce a final document. Flask uses the Jinja template library to render templates.

<!doctype html>
<title>{% block title %}{% endblock %} - Flaskr</title>
<link rel="stylesheet" href="{{ url_for('static', filename='style.css') }}">
    {% if g.user %}
      <li><span>{{ g.user['username'] }}</span>
      <li><a href="{{ url_for('auth.logout') }}">Log Out</a>
    {% else %}
      <li><a href="{{ url_for('auth.register') }}">Register</a>
      <li><a href="{{ url_for('auth.login') }}">Log In</a>
    {% endif %}
<section class="content">
    {% block header %}{% endblock %}
  {% for message in get_flashed_messages() %}
    <div class="flash">{{ message }}</div>
  {% endfor %}
  {% block content %}{% endblock %}
{% extends 'base.html' %}

{% block header %}
  <h1>{% block title %}Register{% endblock %}</h1>
{% endblock %}

{% block content %}
  <form method="post">
    <label for="username">Username</label>
    <input name="username" id="username" required>
    <label for="password">Password</label>
    <input type="password" name="password" id="password" required>
    <input type="submit" value="Register">
{% endblock %}
{% extends 'base.html' %}

{% block header %}
  <h1>{% block title %}Log In{% endblock %}</h1>
{% endblock %}

{% block content %}
  <form method="post">
    <label for="username">Username</label>
    <input name="username" id="username" required>
    <label for="password">Password</label>
    <input type="password" name="password" id="password" required>
    <input type="submit" value="Log In">
{% endblock %}

Suggestion : 5

The render_template() function both selects the template file to be used and passes to it any values or variables it needs.,Line 7: The value for the template variable the_title comes from the render_template() function.,A Flask route function can be long or short, and if it returns render_template() it needs to send not only the name of the template but also the variables required by that template.,If a template uses a lot of variables, it’s much shorter and easier to pass a dictionary to the template than to specify all the individual variables in the render_template() function.

my - flask - app├── static / │└──css / │└──main.css├── templates / │├──index.html│└── student.html├──└──
<h1>{{ pres['President'] }}</h1>

<p>{{ pres['President'] }}, the {{ ord }} president of the
   United States, was born on {{ pres['Birth-date'] }}, in
   {{ pres['Birthplace'] }}. He was {{ pres['Age-when-took-office'] }}
   when he took office on {{ pres['Took-office'] }}. Member:
   {{ pres['Party'] }} Party.</p>
   'Presidency': '16',
   'President': 'Abraham Lincoln',
   'Wikipedia-entry': '',
   'Took-office': '3/4/1861',
   'Left-office': '4/15/1865',
   'Party': 'Republican/National Union',
   'Home-state': 'Illinois',
   'Occupation': 'Lawyer',
   'College': 'None',
   'Age-when-took-office': '52',
   'Birth-date': '2/12/1809',
   'Birthplace': 'LaRue County, Kentucky',
   'Death-date': '4/15/1865',
   'Location-death': 'Washington, D.C.',
   'Image': 'pr16.jpg'
def user(name):
    personal = f'<h1>Hello, {name}!</h1>'
    instruc = '<p>Change the name in the <em>browser address bar</em> \
        and reload the page.</p>'
    return personal + instruc
<h1>Hello, {{ name }}!</h1>

<p>Change the name in the <em>browser address bar</em>
    and reload the page.</p>
   def user(name):
   return render_template('hello.html', name=name)