-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
102 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
Here’s an overview of each of these topics within web development using Python: | ||
|
||
### 11.1 Introduction to Flask | ||
Flask is a lightweight, flexible Python web framework, ideal for beginners and small to medium-sized projects. It’s designed with simplicity and ease of use in mind, allowing developers to create web applications quickly without the heavy scaffolding required by some other frameworks. | ||
|
||
- **Example**: A basic Flask app | ||
```python | ||
from flask import Flask | ||
|
||
app = Flask(__name__) | ||
|
||
@app.route('/') | ||
def home(): | ||
return "Welcome to my Flask app!" | ||
|
||
if __name__ == '__main__': | ||
app.run(debug=True) | ||
``` | ||
This code creates a basic web server that, when accessed, displays "Welcome to my Flask app!" at the root URL (`/`). | ||
|
||
### 11.2 Creating a RESTful API | ||
RESTful APIs use HTTP methods (GET, POST, PUT, DELETE) to interact with resources and are a popular way to enable communication between different parts of a system or with third-party applications. Flask has a module called Flask-RESTful that simplifies API creation. | ||
|
||
- **Example**: A simple REST API for managing items | ||
```python | ||
from flask import Flask, jsonify, request | ||
|
||
app = Flask(__name__) | ||
items = [] | ||
|
||
@app.route('/items', methods=['GET']) | ||
def get_items(): | ||
return jsonify(items) | ||
|
||
@app.route('/items', methods=['POST']) | ||
def create_item(): | ||
new_item = request.json | ||
items.append(new_item) | ||
return jsonify(new_item), 201 | ||
|
||
if __name__ == '__main__': | ||
app.run(debug=True) | ||
``` | ||
Here, `GET` retrieves all items, and `POST` allows new items to be added by sending JSON data. | ||
|
||
### 11.3 Web Development with Django | ||
Django is a robust and feature-rich framework designed for larger applications, offering tools for handling databases, user authentication, and much more. It promotes rapid development and clean, pragmatic design, often requiring minimal setup to create a functional app. | ||
|
||
- **Example**: A Django view rendering a template | ||
```python | ||
# In views.py | ||
from django.shortcuts import render | ||
|
||
def home(request): | ||
return render(request, 'home.html', {'message': 'Welcome to Django!'}) | ||
|
||
# In home.html template | ||
<html> | ||
<body> | ||
<h1>{{ message }}</h1> | ||
</body> | ||
</html> | ||
``` | ||
Django's architecture separates models, views, and templates, allowing developers to organize code better and manage complex applications. | ||
|
||
### 11.4 Authentication and Security | ||
Both Flask and Django support various authentication methods (sessions, tokens, OAuth, etc.) and emphasize security practices such as avoiding SQL injection, using HTTPS, and protecting against cross-site request forgery (CSRF). | ||
|
||
- **Example (Flask authentication)**: Using `flask-login` for session management | ||
```python | ||
from flask import Flask, redirect, url_for | ||
from flask_login import LoginManager, UserMixin, login_user, login_required, logout_user | ||
|
||
app = Flask(__name__) | ||
app.secret_key = 'supersecretkey' | ||
login_manager = LoginManager(app) | ||
|
||
class User(UserMixin): | ||
pass # define user attributes here | ||
|
||
@login_manager.user_loader | ||
def load_user(user_id): | ||
# Load user from database (example: with SQLAlchemy) | ||
return User() | ||
|
||
@app.route('/login') | ||
def login(): | ||
user = User() # Simulate user login | ||
login_user(user) | ||
return redirect(url_for('protected')) | ||
|
||
@app.route('/protected') | ||
@login_required | ||
def protected(): | ||
return "You are logged in!" | ||
|
||
if __name__ == '__main__': | ||
app.run(debug=True) | ||
``` | ||
This example uses `flask-login` to manage sessions and protect routes. For Django, similar functionality can be implemented with built-in authentication views and decorators, like `login_required`. | ||
|
||
Each of these elements builds a solid foundation in Python-based web development. Flask is excellent for lightweight applications and APIs, Django suits larger projects with built-in features, and both provide tools for handling security and authentication securely. |