Jinja Template url_for Function

To build a dynamic website you need multiple re-direction within the website. url_for function is a very handy method that helps in re-direction from one page to another. url_for is also used to link HTML templates with static CSS or JavaScript files. 

In our example since we have multiple choice for example, i.e., variable, if and for. Using url_for, we can create a custom function in which the user can alter the URL to get the specific result. For example, we shall define a function inside app.py and in example 2 we will take link HTML with CSS. 

Syntax of Jinja Template url_for Function

url_for(function_name)

Example 1:

In the below example, if the user enters choice/<his choice> then it will redirect to that HTML file. Make sure redirect and url_for are imported.

Python3




@app.route("/choice/<pick>")
def choice(pick):
    if pick == 'variable':
        return redirect(url_for('var'))
    if pick == 'if':
        return redirect(url_for('ifelse'))
    if pick == 'for':
        return redirect(url_for('for_loop'))


Output:

 

 

Example 2: 

In example 1 we used url_for inside a Python file. Now we shall use url_for inside the layout.html (parent file) HTML file, it will follow the variable define syntax i.e., to be enclosed within {{}}.  Just like templates, create a static file for CSS. 

{{ url_for('static', filename='<path of the file>') }}

HTML




<!DOCTYPE html>
<html>
    <head>
        <title>Template with Jinja2 and Flask</title>
        <link rel="stylesheet" type="text/css" href="{{ url_for('static', filename='style.css') }}">
    </head>
    <body>
        <h1>Welcome to w3wiki</h1>
        <h4>A Computer Science portal for geeks.</h4>
        {% block content %}
        {% endblock %}
    </body>
</html>


Output

 



Templating With Jinja2 in Flask

Flask is a lightweight WSGI framework that is built on Python programming. WSGI simply means Web Server Gateway Interface. Flask is widely used as a backend to develop a fully-fledged Website. And to make a sure website, templating is very important. Flask is supported by inbuilt template support named Jinja2. Jinja2 is one of the most used Web template engines for Python. This Web template engine is a fast, expressive, extensible templating engine. Jinja2 extensively helps to write Python code within the HTML file. Further, it also includes: 

  • Async support for generating templates that automatically handle sync and async functions without extra syntax.
  • Template inheritance and inclusion.
  • The Template engine makes debugging easier.
  • Support of both High-level and Low-level API support.

Install the required package

To install the Jinja2 package in Python, check your latest pip version and stay updated. Install Jinja2 using the following command: 

pip install Jinja2

But since we are dealing with the Templating with Jinja2 in Flask, there is no need to separately install Jinja2. When you install the Flask framework, the Jinja2 comes installed with it. 

pip install flask

Similar Reads

Templating with Jinja2 in Flask

Before we proceed with the coding part, this is how our project directory should look like:...

Main Python File

Here is the common app.py file that interfaces with all the HTML files....

Jinja Template Variables

...

Jinja Template if Statements

To declare the variable using Jinja Template we use {{variable_name}} within the HTML file. As a result, the variable will be displayed on the Website....

Jinja Template for Loop

...

Jinja Template Inheritance

Just like declaring the variable in the Jinja2 template, if conditions have almost similar syntax. But here we specify the beginning and end of the if block....

Jinja Template url_for Function

...

Contact Us