Python Track · Part 9 of 9

Python Web Development: Flask and Django Basics

Python
Web Development
Flask
An introduction to Python web frameworks Flask and Django: routing, templates, databases, and HTTP requests and responses.
Author

Gujuri Alekha

Published

Oct 20, 2024

9.1 Introduction to Web Frameworks

  • Flask: Flask is a lightweight and micro web framework for Python. It is known for its simplicity and flexibility, making it a great choice for small to medium-sized projects. Flask provides the essential tools to build web applications, including routing, templates, and support for handling HTTP requests and responses.
  • Django: Django, on the other hand, is a high-level and feature-rich web framework that follows the “batteries-included” philosophy. It provides many built-in features for web development, such as an Object-Relational Mapping (ORM) system, an admin interface, and robust security features. Django is a good choice for larger, more complex projects.

9.2 Creating Web Applications

  • Routing: Web frameworks allow you to define URL routes, which map specific URLs to functions in your Python code. For example, you can specify that when a user visits “/home,” a particular function should be executed to handle that request.
  • Templates: Web frameworks typically support templates that allow you to separate the HTML and Python code. This separation makes it easier to maintain and modify the user interface of your web application.
  • Database Integration: You can connect your web application to a database using the framework’s database tools. This enables you to store and retrieve data, making your web application dynamic and interactive.

9.3 Handling HTTP Requests and Responses

  • HTTP Requests: Web applications receive HTTP requests from clients (usually web browsers). These requests can include various methods (e.g., GET, POST, PUT) and data (form data, query parameters). Your Python code can extract and process this information to determine how to respond.
  • HTTP Responses: After processing a request, your web application generates an HTTP response to send back to the client. This response includes data (HTML, JSON, etc.) and metadata (status codes, headers) that inform the client about the result of the request.
Show code
from flask import Flask

app = Flask(__name)

@app.route('/')
def hello_world():
    return 'Hello, World!'

if __name__ == '__main__':
    app.run()

Citation

BibTeX citation:
@online{alekha2024,
  author = {Alekha, Gujuri},
  title = {Python {Web} {Development:} {Flask} and {Django} {Basics}},
  date = {2024-10-20},
  url = {https://gujurialekha.github.io/blog/python/09-python-web-development.html},
  langid = {en}
}