Python Web Development: A Quick Start with the Lightweight Flask Framework
This article introduces the basic content of Flask, a lightweight Python web framework, including: **Definition and Features**: Flask is a lightweight and flexible Python framework that encapsulates repetitive tasks (such as HTTP handling and route management). It has a low learning curve, strong scalability, and is suitable for quickly developing small websites or APIs. **Environment Setup**: Install it via `pip install flask` and verify the version with `flask --version`. **First Application**: Create `app.py` to instantiate Flask, define the root route with `@app.route('/')`, and start the server with `app.run(debug=True)`. Accessing `http://127.0.0.1:5000` will display "Hello, Flask!". **Routes and View Functions**: Support basic routes (e.g., `/about`) and dynamic parameters (e.g., `/user/<username>`). Parameter types include integers, paths, etc. (e.g., `/post/<int:post_id>`). **Templates and Static Files**: Use the Jinja2 template engine for dynamic rendering of variables, loops, and conditions (in the `templates` folder); static resources (CSS/JS) are placed in the `static` folder, accessed via `url_for('static', filename='...')`.
Read MoreFlask Introduction: Mastering Routes and View Functions from Scratch
This article is an introductory guide to Flask routes and view functions. First, install Flask using `pip install flask`. A basic example (code in `app.py`) demonstrates the first application: create a `Flask` instance, define the root route with `@app.route('/')`, and have the view function `index()` return "Hello, Flask!". After running, access `http://127.0.0.1:5000/` to view the result. Routes map URLs to view functions, categorized into two types: static routes (e.g., `/about` bound to the `about()` function) and dynamic routes (using `<parameter_name>`, e.g., `/user/<username>`, supporting type constraints like `int:post_id`). View functions process requests: they can return strings, HTML, and support HTTP methods (e.g., GET/POST) via the `methods` parameter. To return JSON, use `jsonify`. Start the development server with `app.run(debug=True)` for easier debugging. Key points: Route definition mapping, dynamic parameter handling for variable paths, view functions processing requests and returning responses (text, HTML, JSON, etc.), and specifying HTTP methods through `methods`. Mastering these enables building simple web applications, with subsequent topics to explore templates and static files.
Read MoreDjango from Scratch: Build a Simple Blog System with ORM and Template Engine in 3 Steps
This article introduces how to quickly build a blog system displaying article lists using Django, with a core understanding of ORM operations for data and template rendering for pages. Step 1: Environment preparation and project initialization. After installing Django, create the project `myblog` and the app `blog`. The project structure includes configuration directories, app directories, and command-line tools. Step 2: Define data models using ORM. Write a `Post` class (with fields: title, content, publication time) in `blog/models.py`, which is automatically mapped to a database table. Activate the model (configure `settings.py`) and execute migrations to generate the table. Step 3: Views and template rendering. Write a view function in `views.py` to retrieve article data and configure routing to distribute requests. Render the article list in the template `index.html` using Django template syntax, supporting loops and variable output. Running `python manage.py runserver` allows access to the blog. The core is to master Django's ORM model definition, view processing, and template rendering processes, with potential for subsequent feature expansion.
Read MoreStep-by-Step Guide: Flask Routes and View Functions, Build Your First Web Page in 10 Minutes
Flask is a lightweight Python Web framework, simple and flexible, suitable for beginners, and supports extensibility as needed. Installation requires Python 3.6+, and can be done via `pip install flask`. To verify, use `flask --version`. The core of a basic application: Import the Flask class and instantiate an `app` object; define the root route with `@app.route('/')`, binding to the view function `home()`, which returns content (e.g., "Hello, Flask!"); `app.run()` starts the development server (default port 5000). For advanced support, dynamic routing is available, such as `/user/<username>`, where the view function receives parameters to implement personalized responses, supporting types like `int` and `float`. Core concepts: Routes bind URLs to functions, view functions handle requests and return content, and `app.run()` starts the service. Key techniques: `if __name__ == '__main__'` ensures the service starts when the script is run directly, and dynamic routing enhances page flexibility.
Read More