Php Chinese network (www.php.cn) provides the most comprehensive basic tutorial on programming technology, introducing HTML, CSS, Javascript, Python, Java, Ruby, C, PHP, basic knowledge of MySQL and other programming languages. At the same time, this site also provides a large number of online instances, through which you can better learn programming... Reply content: laxatives!
I didn't see the source code related to form, but it should be like this:
First, you must understand the basic running mechanism of MVC (or MTC) such as flask.
-For the view of flask, you need to know the wsgi protocol (if you do not know it, Google it yourself ).
HTTP utils (flask uses werkzeug) at the underlying layer (logical underlying) to parse HTTP requests at the client end, and construct it as an environment of wsgi (including request and other information ).
In the process request, wsgi server constructs environ based on the wsgi protocol and passes it into the flask app instance (this is the wsgi app implemented by the flask framework ), the flask app instance uses this environ and its own ''start _ response' method (which is also the uwsgi protocol Protocol) to complete request processing and response.
Flask has a line-level (or greenlet) request object. Before the real process response, I threw environ into this request object. then the request followed your thread and became a tacit gunner ~~ Until response is finished or the thread is finished.
What is the role of a batch of route in views? This is the url routing, that is, I clicked a specific url on your site, how can flask meet you. You have to say what you want.
So when will views be used? Nonsense, of course, it is still before receiving and understanding the request to respond.
Okay, the flask app instance obtains the url of your request from the request object when processing your needs. this is equivalent to a key, then the app instance finds the corresponding lock in views based on the key (the processing logic you want is the function under route in views). What about this lock, once she is opened, it's time to satisfy your needs ~ ~
P.S. as for how to match the lock, this is url mapping, which is a bunch of regular matching (don't underestimate this, if you have used flask blueprint, you will understand that writing such a mapping is a great task)
-What about model & controller?
Your requirement is actually to give you the response by flask, and the response is actually the data (both in the restful api and template form are the data you export ).
How does the data come from? This is what you get from the database (in the broad sense, including SQL & nosql db, memory-level cache, disk files, etc.). as for the data source, it may be your own plug-in, or from somewhere else.
The model is just doing this, but it is abstract and converts database operations into operations on Python objects (this is the well-known ORM, but it is actually useless, if you forget to write more ORM than query optimization, if you have high performance requirements and a lot of time, you can use your own explain efficient retrieval solution, I am not rarely put into this trap % >_<% ).
You can actually not use controller. however, to make logical separation, you can use it to make your business more elegant.
Okay, I have said so much. I don't know if anyone can understand it. if not, let me think about it for a moment ~ ~
========================================================== ======================================
In short, when you use flask for application, MVC performs its respective duties to pile up your data generation logic. you don't need to worry about anything else.
Finally, you can go back to your problem:
Let's take a look at Form:
from flask.ext.wtf import Formfrom wtforms import StringField, SubmitFieldfrom wtforms.validators import Requiredclass NameForm(Form): name = StringField('What is your name?', validators=[Required()]) submit = SubmitField('Submit')
The request of Flask is a global variable (specifically a local variable of the thread, which can be regarded as a global variable in the thread). I didn't read the code related to the form of Flask. I guess it all here, it should be the NameForm class that directly uses the request. from this POST form Data Dictionary for verification, and because the request is global within the thread, all nameforms can be used to obtain form data NameForm () without passing the parameter () it should be inherited from WTForm. First, let's take a look at this.
I was also confused at the beginning. the flask_wtf module is used in this flask web development, while the WTForms module is used in some tutorials. This is the form verification routing function of WTForms @ app. route ('/register', methods = ['GET', 'post']) def register (): form = RegistrationForm (request. form) if request. method = 'post' and form. validate (): You can see three differences between pass and pass. request is used for RegistrationForm instantiation. form, which is not used in your example. 2. in your example, the POST or GET method is not verified. the request is used in the above example. method = 'post' 3. one is form. validate (), one is form. validate_on_submit (). The Form class in the flask_wtf module encapsulates the Form class in WTForms. Encapsulate request. form and request. method = 'post. The code can be easily understood.
Try to simplify the explanation:
1. when a route triggers the index View function.
The form = NameForm () statement creates a form instance object, that is, the form is created.
2. if this is the first time you access a page containing this form, that is, a GET request
If form. validate_on_submit () is false, and the following statement blocks are not executed.
3. for POST requests
At this time, the form already exists.
If form. validate_on_submit () is true, the following statement block is executed.
Name = form. name. data gets the form attribute value during POST.
4. Whether POST or GET
Return render_template('index.html ', form = form, name = name)
This statement must be executed.
For details about how to obtain the form value, see flask. request.
ClassFlask. requestAPI-Flask Documentation (0.10)
To access incoming request data, you can use the global requestobject. flask parses incoming request data for you and gives youaccess to it through that global object. internally Flask makessure that you always get the correct data for the active thread if youare in a multithreaded environment.
This is a proxy. See Notes On ProxiesFor more information.
The request object is an instance of a RequestSubclass and provides all of the attributes Werkzeug defines. Thisjust shows a quick overview of the most important ones.
Can look at @ wang Harvey and @ Zhu Tianyi's answer first, then look at the Flask-WTF definition Form class source code, in the Form class annotation, https://github.com/lepture/flask-wtf/blob/master/flask_wtf/form.py#L41-L42
These two lines should be able to clearly explain the problem of the subject. Here we reference:
If formdata is not specified, this will use flask.request.form.Explicitly pass formdata = None to prevent this.
= Although @ wang harvey's answer is very good, I don't think it solves the problem of the landlord at all ............ No, @ cannot answer the highest ticket ......
The landlord does not know how the name came out? The name is defined. Python is strongly typed and directly defines the name to store the data of form. name. data.
So how does form. name. data come out? Form. name is a data defined in form. its name (see [documentation] Forms-WTForms 2.0.3dev documentation) ) Is name (defined by yourself ).
So how can we pass form. name. data to name? We can see that it is redirected to index.html, so we open index.html (for this reason, I also opened a file checkout that has been useless for a long time), we can see that
Hello, {% if name %}{{ name }}{% else %}Stranger{% endif %}!
In my personal understanding, the first request is a get request that renders an empty table page, click submit to submit the second request and post the request. at this time, the table content is sent to the server and assigned a value. Subject: I use the same book as you. I am a self-taught program, and I have read the basic python syntax to better understand this book. At the very beginning, no html will actually be used ...... I am sorry to see Chapter 6 and chapter 7 after more than two months...