Flask is a micro web framework for Python, known for its simplicity and flexibility. It allows you to build web applications quickly with minimal setup.
-
Routing in Flask is used to connect web page URLs to Python functions.
-
Routes are created using the
@app.routedecorator.from flask import Flask app = Flask(__name__) @app.route('/') def home(): return 'Hello, World!' if __name__ == '__main__': app.run()
-
Flask uses Jinja2 template engine.
-
Templates are HTML files that allow for dynamic content to be inserted.
<!-- templates/home.html --> <h1>Hello, {{ name }}!</h1>
from flask import render_template @app.route('/hello/<name>') def hello(name): return render_template('home.html', name=name)
Django is a high-level Python web framework that encourages rapid development and clean, pragmatic design. It's known for its "batteries-included" approach.
-
In Django, URL patterns are defined in a module called
urls.py. -
Routes are matched to views, which handle the business logic and render a response.
# urls.py from django.urls import path from . import views urlpatterns = [ path('hello/', views.hello, name='hello'), ]
# views.py from django.http import HttpResponse def hello(request): return HttpResponse("Hello, World!")
- Django also uses the Jinja2 template engine.
- Templates in Django are used similarly to Flask, with dynamic content being rendered through the template engine.
-
Django ORM is a powerful tool for database interaction.
-
Define your data models in Python, and Django handles the database schema and queries.
from django.db import models class Person(models.Model): first_name = models.CharField(max_length=30) last_name = models.CharField(max_length=30)
Both Flask and Django provide robust solutions for web development in Python, with Flask being more flexible and minimalistic, while Django offers a more feature-rich framework. Understanding routing, templates, and ORM is essential for building web applications with these frameworks. Flask might be more suitable for smaller projects or when more control is desired, whereas Django is better suited for larger applications with more out-of-the-box features.