Flask 入门教程(李辉) (it-ebooks)(Z-Library)
Web Technology
No Description
231
Views
0
Downloads
0.00
Total Donations
AI Guide
AI Reading Assistant
Whole-book reading guide from stratified index samples; jump to passages in the text
AI guide
【One-Line Pitch】
A hands-on, minimal-introduction tutorial that walks you through building a complete "Watchlist" movie-tracking web app with Flask, covering everything from environment setup to deployment—ideal for Python beginners who want to learn web development by doing.
【Book Arc】
- **Opening (~0%–10%)**: Covers prerequisites, tool installation (editor, browser, Git), command-line basics, and project scaffolding—including creating a Git repository and optionally pushing to GitHub.
- **Early (~10%–23%)**: Introduces the core Flask concepts: creating a Flask app, defining routes and view functions, running the development server, and using Jinja2 templates to render dynamic HTML pages with variables, filters, and loops.
- **Early–Middle (~23%–32%)**: Adds static files (CSS, images) to style the page, then introduces database integration with SQLite and SQLAlchemy—defining model classes, configuring the database URI, and performing CRUD operations.
- **Middle (~32%–48%)**: Moves from fake data to real database queries in views, creates a custom CLI command to generate fake data, and optimizes templates using context processors and template inheritance to reduce duplication.
- **Late (~48%–75%)**: Implements forms for adding/editing/deleting movie entries, adds user authentication (login/logout), and introduces testing to verify app behavior.
- **Ending (~75%–100%)**: Covers code organization (blueprints, application factory) and deployment to production, replacing the development server with a production WSGI server.
【Key Takeaways】
- **Flask is a micro-framework built on Werkzeug and Jinja2** (Early): It handles request-response cycles and template rendering, keeping the core minimal while letting you add extensions as needed.
- **Routes and view functions are the backbone of a Flask app** (Early): Using `@app.route()` decorators binds URLs to Python functions; the return value becomes the HTTP response shown in the browser.
- **Jinja2 templates separate logic from presentation** (Early): Use `{{ }}` for variables, `{% %}` for statements like `if`/`for`, and `{# #}` for comments; filters like `length` process variables inline.
- **SQLAlchemy ORM simplifies database work** (Early–Middle): Define Python classes as models (tables), use `db.session.add()` and `db.session.commit()` to persist changes, and query with methods like `filter_by()` and `first()`.
- **Context processors inject global template variables** (Middle): A function decorated with `@app.context_processor` returns a dict that becomes available in every template, eliminating repetitive variable passing.
- **Template inheritance reduces duplication** (Middle): Define a base template with `{% block %}` sections; child templates use `{% extends %}` and override blocks to reuse common HTML structure.
- **Custom CLI commands streamline development** (Middle): Using `@app.cli.command()`, you can create commands like `flask forge` to generate fake data, making testing and development faster.
- **Production deployment requires a real WSGI server** (Ending): The built-in development server is for local use only; switch to a production-grade server when going live.
【Reading Tips】
- **Type every command and code block yourself**—the book explicitly emphasizes this; it reinforces learning and helps you catch environment-specific issues early.
- **Skim the "进阶提示" (advanced tips) sections** if you're a beginner; they point to external resources and deeper topics but aren't required for the core tutorial.
- **Pay extra attention to Chapter 5 (Database)**—it's the biggest conceptual leap; practice the CRUD operations in the Python shell before integrating them into views.
- **Use the GitHub commit references** provided at the end of each chapter to compare your code against the official example if you get stuck.
- **Don't skip the final deployment chapter**—even if you don't deploy immediately, it clarifies how Flask apps run in production and why the dev server isn't sufficient.
【Coverage Limits】
This guide covers the full arc from setup to deployment, but detailed code listings for forms, authentication, testing, and deployment are not included in the excerpts—refer to the book for those chapters.
Passage locations
Excerpt 1
E(集成开发环境),比如 PyCharm ;也可以选择相对轻量的编辑器,比如 Atom 或 Sublime Text 。浏览器建议使用 Firefox 或 Chrome 。 使用命令行 在本书中,你需要使用命令行窗口来执行许多操作。你可以使用 Windows 下的 cmd.exe,或是 macOS 和 Linux...
View in text
Excerpt 2
L 规则 另外,你也可以自由修改传入 app.route 装饰器里的 URL 规则字符串,但要注意以斜线 / 作为开头。比如: @app.route('/home') def hello () : return 'Welcome to My Watchlist!' 保存修改,这时刷新浏览器,则会看到一个 404...
View in text
Excerpt 3
库 为了简化数据库操作,我们将使用 SQLAlchemy ——一个 Python 数据库工具(ORM,即对象关系映射)。借助 SQLAlchemy,你可以通过定义 Python 类来表示数据库里的一张表(类属性表示表中的字段 / 列),通过对这个类进行各种操作来代替写 SQL 语句。这个类我们称之为 模型类 ,类...
View in text
Excerpt 4
第 6 章:模板优化 这一章我们会继续完善模板,学习几个非常实用的模板编写技巧,为下一章实现创建、编辑电影条目打下基础。 自定义错误页面 为了引出相关知识点,我们首先要为 Watchlist 编写一个错误页面。目前的程序中,如果你访问一个不存在的 URL,比如 /hello,Flask 会自动返回一个 404 错...
View in text
Recommended for You
{{#thumbnailUrl}}
{{/thumbnailUrl}}
{{^thumbnailUrl}}
{{/thumbnailUrl}}
Loading recommended books...
Failed to load, please try again later
Tip the Site
Scan the WeChat Pay or Alipay code to tip. No login required.
WeChat Pay
Alipay