Flask Jinja Guide ASCII Cleaned
Flask Jinja Guide ASCII Cleaned
1. Introduction to Flask
What is Flask?
Flask is a lightweight framework in Python used to create web applications. Its simple, flexible, and allows you to build
Example: Flask helps you create a website that shows user data or manages forms.
Flask is simpler and gives you more control, while Django comes with many features ready-made (like authentication,
admin panel). Flask is good for learning and small projects; Django is better for big, complex apps.
Installing Flask
Project structure
myproject/
app = Flask(__name__)
@app.route("/")
def home():
if __name__ == "__main__":
app.run(debug=True)
Run it:
python app.py
3. Routing in Flask
Basic routes
@app.route("/about")
def about():
Dynamic routes
@app.route("/user/<username>")
def user_profile(username):
URL converters
@app.route("/post/<int:post_id>")
def post(post_id):
What is Jinja?
Jinja is a templating language that allows you to embed Python code in HTML.
Rendering templates
Example:
Flask and Jinja Template Guide
@app.route("/")
def home():
return render_template("home.html")
Template inheritance
<html>
<body>
</body>
</html>
{% extends "base.html" %}
{% block content %}
<h1>Welcome to My Site</h1>
{% endblock %}
Flask and Jinja Template Guide
Template variables
@app.route("/")
def home():
Access it in Jinja: