Web Development - CameronAuler/python-devops GitHub Wiki

Python is widely used for web development, offering frameworks like Django (for large-scale applications) and Flask (for lightweight, flexible applications).

Table of Contents

Web Frameworks (Django & Flask)

Django (Full-Stack Web Framework)

Django is mainly used for large-scale applications with built-in tools such as E-commerce platforms, CMS, social media applications. It offers built-in ORM, authentication, admin panel, and security and it follows the Model-View-Template (MVT) pattern.

Installing Django

pip install django

Creating a Django project

Visit: http://127.0.0.1:8000/ to see your Django app running.

django-admin startproject myproject
cd myproject
python manage.py runserver

Flask (Minimalist web Framework)

Flask is mainly used for small projects, microservices, APIs, specifically, prototyping, lightweight web apps, and REST APIs. It is lightweight and flexible and it uses Jinja2 templating for rendering HTML.

Installing Flask

pip install flask

Creating a Flask App

Visit: http://127.0.0.1:5000/ to see your Flask app running.

from flask import Flask

app = Flask(__name__)

@app.route("/")
def home():
    return "Hello, Flask!"

if __name__ == "__main__":
    app.run(debug=True)

Building RESTful APIs

RESTful APIs allow communication between clients and servers using HTTP methods.

Flask REST API

Mainly used for building APIs for mobile apps, front-end apps, or third-party integrations. Features include:

  • GET /users β†’ Fetch all users.
  • GET /users/<id> β†’ Fetch a specific user.
  • POST /users β†’ Add a new user.
from flask import Flask, jsonify, request

app = Flask(__name__)

# In-memory database
users = [{"id": 1, "name": "Alice"}, {"id": 2, "name": "Bob"}]

@app.route("/users", methods=["GET"])
def get_users():
    return jsonify(users)

@app.route("/users/<int:user_id>", methods=["GET"])
def get_user(user_id):
    user = next((u for u in users if u["id"] == user_id), None)
    return jsonify(user) if user else ("User not found", 404)

@app.route("/users", methods=["POST"])
def add_user():
    new_user = request.json
    users.append(new_user)
    return jsonify(new_user), 201

if __name__ == "__main__":
    app.run(debug=True)

Django REST API

Django provides Django REST Framework (DRF) for API development. It is mainly used for enterprise-level APIs with authentication, permissions, and serialization.

Installing DRF

pip install djangorestframework

Adding DRF to Django

  • Modify settings.py:
INSTALLED_APPS = [
    "rest_framework",
    "myapp",
]

Creating the Django REST API

from rest_framework.response import Response
from rest_framework.decorators import api_view

@api_view(["GET"])
def api_home(request):
    return Response({"message": "Hello, Django API!"})

Template Rendering

Templates allow dynamic HTML generation using placeholders.

Flask Template Rendering

Flask uses Jinja2 for rendering templates which allows variables ({{ name }}), Loops ({% for item in list %}), and Conditionals.

Project Structure

/myapp
  β”œβ”€β”€ app.py
  β”œβ”€β”€ templates/
  β”‚   β”œβ”€β”€ home.html

home.html (Jinja2 template)

<!DOCTYPE html>
<html>
<head><title>Flask Template</title></head>
<body>
    <h1>Hello, {{ name }}!</h1>
</body>
</html>

Flask View (app.py)

Visit: http://127.0.0.1:5000/

from flask import Flask, render_template

app = Flask(__name__)

@app.route("/")
def home():
    return render_template("home.html", name="Alice")

if __name__ == "__main__":
    app.run(debug=True)

Django Template Rendering

Django’s template engine works similarly to Flask. It is mainly used for rendering dynamic HTML pages in both Flask & Django.

Project Structure

/myproject
  β”œβ”€β”€ myapp/
  β”‚   β”œβ”€β”€ views.py
  β”‚   β”œβ”€β”€ templates/
  β”‚   β”‚   β”œβ”€β”€ home.html

home.html

<!DOCTYPE html>
<html>
<head><title>Django Template</title></head>
<body>
    <h1>Hello, {{ name }}!</h1>
</body>
</html>

Django View(views.py)

from django.shortcuts import render

def home(request):
    return render(request, "home.html", {"name": "Alice"})
⚠️ **GitHub.com Fallback** ⚠️