Render Template in Python Flask



Source Code

main.py

from flask import Flask, render_template
 
app = Flask(__name__)
 
 
@app.route('/')
@app.route('/index')
def index():
    courses = ["C", "C++", "Java", "Python", "HTML", "CSS"]
    is_logged_in = False
    return render_template("index.html", courses=courses, is_logged_in=is_logged_in)
 
 
@app.route('/about')
def about():
    return '<h3>Tutor Joes Computer Education Salem</h3>'
 
 
@app.route('/contact')
def contact():
    return '<h3>Contact Us</h3>'
 
 
@app.route('/users/<name>')
def users(name):
    # return  "<h3>Welcome {}</h3>".format(name.upper())
    fruits = ["Apple", "Orange", "Pineapple"]
    profile = {"father": "Joes", "age": 25, "City": "Salem"}
    return render_template("users.html", user_name=name, fruits=fruits, profile=profile)
 
 
if __name__ == '__main__':
    app.run(debug=True)
 

user.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<h2>Welcome {{user_name}}</h2>
<p>{{fruits}}</p>
<p>{{fruits[0]}}</p>
<p>{{fruits[1:]}}</p>
<p>{{profile}}</p>
<p>Age : {{profile["age"]}}</p>
</body>
</html>

output

index.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<h1>Tutor Joe's Flask Tutorial</h1>
<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Doloribus possimus repellendus tempore vitae. Ad amet
    aspernatur blanditiis delectus dignissimos, doloremque ducimus eius explicabo ipsum minus possimus praesentium,
    reiciendis sapiente sint.</p>
{% if is_logged_in %}
<h3>List of Computer Courses</h3>
<ul>
    {% for course in courses %}
    <li>{{course}}</li>
    {% endfor %}
</ul>
{% else %}
<p>Please log in to access..!</p>
{% endif %}
</body>
</html>

output

output