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).
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.
pip install django
Visit: http://127.0.0.1:8000/ to see your Django app running.
django-admin startproject myproject
cd myproject
python manage.py runserver
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.
pip install flask
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)
RESTful APIs allow communication between clients and servers using HTTP methods.
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 provides Django REST Framework (DRF) for API development. It is mainly used for enterprise-level APIs with authentication, permissions, and serialization.
pip install djangorestframework
- Modify
settings.py
:
INSTALLED_APPS = [
"rest_framework",
"myapp",
]
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!"})
Templates allow dynamic HTML generation using placeholders.
Flask uses Jinja2 for rendering templates which allows variables ({{ name }}), Loops ({% for item in list %}), and Conditionals.
/myapp
βββ app.py
βββ templates/
β βββ home.html
<!DOCTYPE html>
<html>
<head><title>Flask Template</title></head>
<body>
<h1>Hello, {{ name }}!</h1>
</body>
</html>
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βs template engine works similarly to Flask. It is mainly used for rendering dynamic HTML pages in both Flask & Django.
/myproject
βββ myapp/
β βββ views.py
β βββ templates/
β β βββ home.html
<!DOCTYPE html>
<html>
<head><title>Django Template</title></head>
<body>
<h1>Hello, {{ name }}!</h1>
</body>
</html>
from django.shortcuts import render
def home(request):
return render(request, "home.html", {"name": "Alice"})