DAY #27
1.主题
Flask Web Development.pdf
原作者视频讲解
https://www.bilibili.com/video/av5708093/?p=9
2.摘要
一个application的三部分:
第一部分:Initialization
All Flask applications must create an application instance. The web server passes all requests it receives from clients to this object for handling, using a protocol called Web Server Gateway Interface (WSGI). The application instance is an object of class Flask, usually created as follows:
from flask import Flask
app = Flask(__name__)
第二部分:Routes and View Functions
Clients such as web browsers send requests to the web server, which in turn sends them to the Flask application instance. The application instance needs to know what code needs to run for each URL requested, so it keeps a mapping of URLs to Python functions. The association between a URL and the function that handles it is called a route.
The most convenient way to define a route in a Flask application is through the app.route decorator exposed by the application instance, which registers the decorated function as a route. The following example shows how a route is declared using this decorator:
@app.route('/')
def index():
return '<h1>Hello World!</h1>'
Flask supports these types of URLs using a special syntax in the route decorator.
@app.route('/user/<name>') def user(name):
return '<h1>Hello, %s!</h1>' % name
The portion enclosed in angle brackets is the dynamic part, so any URLs that match the static portions will be mapped to this route. When the view function is invoked, Flask sends the dynamic component as an argument. In the earlier example view function, this argument is used to generate a personalized greeting as a response.
第三部分:Server Startup
The application instance has a run method that launches Flask’s integrated development web server:
if __name__ == '__main__':
app.run(debug=True)
During development, it is convenient to enable debug mode, which among other things activates the debugger and the reloader. This is done by passing the argument debug set to True.
逻辑总结:
The Request-Response Cycle = Initialization + Routes and View Functions + Server Startup
Contexts enable Flask to make certain variables globally accessible to a thread without interfering with the other threads.
There are two contexts in Flask: the application context and the request context.
If any of these variables are accessed without an active application or request context, an error is generated.
Flask attaches methods to each route so that different request methods sent to the same URL can be handled by different view functions.
In most cases the response is a simple string that is sent back to the client as an HTML page.
A template is a file that contains the text of a response, with placeholder variables for the dynamic parts that will be known only in the context of a request. The process that replaces the variables with actual values and returns a final response string is called rendering. For the task of rendering templates, Flask uses a powerful template engine called Jinja2.
In its simplest form, a Jinja2 template is a file that contains the text of a response.
Bootstrap is a client-side framework, so the server is not directly involved with it. All the server needs to do is provide HTML responses that reference Bootstrap’s cascading style sheets (CSS) and JavaScript files and instantiate the desired components through HTML, CSS, and JavaScript code. The ideal place to do all this is in templates.
A redirect is a special type of response that has a URL instead of a string with HTML code. When the browser receives this response, it issues a GET request for the redirect URL, and that is the page that is displayed. This trick is known as the Post/ Redirect/Get pattern.
In the context of an ORM, a model is typically a Python class with attributes that match the columns of a corresponding database table.
Project Structure
This structure has four top-level folders:
* The Flask application lives inside a package generically named app.
* The migrations folder contains the database migration scripts, as before.
* Unit tests are written in a tests package.
* The venv folder contains the Python virtual environment, as before.
3.心得感悟
粗读前半部分,获取三个框架:
整体项目框架 == app + migrations + tests + venv
单个application的框架 == Initialization + Routes and View Functions + Server Startup
整体结构框架 == Ttemplates + Web Forms + Databases + Email
了解基本运行原理,懂得多一点挺好。
这本书的整体思路不错,是一点点一层层展开讲解,能够白手起家读懂一个网页从小到大怎么发育长大成人的,不错。
但是需要补充javascript的知识。。。~
这个波兰英语,听着实在费劲!~~
4.时间统计
昨日阅读5小时,累计535小时