Rumah > Artikel > pembangunan bahagian belakang > Flask表单疑问,这个name是怎么传进来的?
<code class="language-text">from flask.ext.wtf import Form
from wtforms import StringField, SubmitField
from wtforms.validators import Required
class NameForm(Form):
name = StringField('What is your name?', validators=[Required()])
submit = SubmitField('Submit')
</code>
Flask的request是全局变量(准确点说是线程局部变量,在线程内可以看做是全局变量),我没有看Flask的form相关的代码,所有这里猜测一下,应该是NameForm这个类直接使用到了request.from这个POST表单数据的字典来验证,又因为request在线程内是全局的,所有NameForm可以不通过参数就可用获取到表单数据
NameForm() 应该是继承自 WTForm ,先了解一下这个东西。
<code class="language-python"><span class="err">我当初也被搞糊涂了,那本</span> <span class="n">flask</span> <span class="n">web</span> <span class="n">development</span><span class="err">里用到的是</span><span class="n">flask_wtf</span><span class="err">模块,而有的教程里用到的却是</span><span class="n">WTForms</span><span class="err">模块。</span>
<span class="err">这是</span><span class="n">WTForms</span><span class="err">的表单验证路由函数</span>
<span class="nd">@app.route</span><span class="p">(</span><span class="s">'/register'</span><span class="p">,</span> <span class="n">methods</span><span class="o">=</span><span class="p">[</span><span class="s">'GET'</span><span class="p">,</span> <span class="s">'POST'</span><span class="p">])</span>
<span class="k">def</span> <span class="nf">register</span><span class="p">():</span>
<span class="n">form</span> <span class="o">=</span> <span class="n">RegistrationForm</span><span class="p">(</span><span class="n">request</span><span class="o">.</span><span class="n">form</span><span class="p">)</span>
<span class="k">if</span> <span class="n">request</span><span class="o">.</span><span class="n">method</span> <span class="o">==</span> <span class="s">'POST'</span> <span class="ow">and</span> <span class="n">form</span><span class="o">.</span><span class="n">validate</span><span class="p">():</span>
<span class="k">pass</span>
<span class="err">对比二者你会发现,二者有三个区别</span>
<span class="mf">1.</span><span class="n">RegistrationForm</span><span class="err">实例化时用到了</span><span class="n">request</span><span class="o">.</span><span class="n">form</span><span class="err">,而你的例子里没有用到。</span>
<span class="mf">2.</span><span class="err">你的例子里没有验证表单是</span><span class="n">POST</span><span class="err">还是</span><span class="n">GET</span><span class="err">方法,上面这个例子里用了</span><span class="n">request</span><span class="o">.</span><span class="n">method</span> <span class="o">==</span> <span class="s">'POST'</span>
<span class="mf">3.</span><span class="err">一个是</span><span class="n">form</span><span class="o">.</span><span class="n">validate</span><span class="p">()</span><span class="err">,一个是</span><span class="n">form</span><span class="o">.</span><span class="n">validate_on_submit</span><span class="p">()</span><span class="err">。</span>
<span class="err">可见</span><span class="n">flask_wtf</span><span class="err">模块里的</span><span class="n">Form</span> <span class="err">类对</span><span class="n">WTForms</span><span class="err">里的</span><span class="n">Form</span><span class="err">类进行了封装。</span>
<span class="err">把</span><span class="n">request</span><span class="o">.</span><span class="n">form</span> <span class="err">和</span><span class="n">request</span><span class="o">.</span><span class="n">method</span> <span class="o">==</span> <span class="s">'POST'</span><span class="err">封装进去了。对比一下代码应该很容易明白。</span>
</code>
试图简化解释一下这个问题:To access incoming request data, you can use the global request object. Flask parses incoming request data for you and gives you access to it through that global object. Internally Flask makes sure that you always get the correct data for the active thread if you are in a multithreaded environment.
This is a proxy. See Notes On Proxies for more information.
The request object is an instance of a Request subclass and provides all of the attributes Werkzeug defines. This just shows a quick overview of the most important ones.
可以先看看 @wang Harvey 和 @朱添一 的回答,再去看一下Flask-WTF中定义Form类的源码,在Form类的注释里,https://github.com/lepture/flask-wtf/blob/master/flask_wtf/form.py#L41-L42 这两行应该能够清楚解释题主的问题了。在此引用一下:
<code class="language-text">If formdata is not specified, this will use flask.request.form.
Explicitly pass formdata = None to prevent this.
</code>
== 虽然@wang harvey 的答案非常棒,但我觉得完全没解决楼主的问题啊…………不行,@不上最高票答主了……
楼主不知道 name 是怎么出来的?那么 name 是定义出来的。python是强类型,直接定义了 name 来存储 form.name.data 的数据。
那么 form.name.data 是怎么出来的?form.name是你在 form 里面定义的一个数据,它的 name(参见[文档]Forms — WTForms 2.0.3dev documentation )就是 name(你自己定义的) 。
那么我们把 form.name.data 传给 name ,有什么用呢?我们看它是重定向到 index.html 里面的,那么我们打开 index.html(为了这个我还专门打开好长时间都没用的文件 checkout 了一把),我们可以看到
<code class="language-text"> <h1>Hello, {% if name %}{{ name }}{% else %}Stranger{% endif %}!</h1>
</code>
以下是我的个人理解,首先第一次请求的时候,是get请求,渲染了一个空的表格页面,当你填完了东西之后,点击提交就已经是第二次请求,post请求,这时候就把表格的内容发送给服务器,再进行赋值。
题主,我跟你用的同一本书。我是自学程序,看了基本的python语法就强撸这本书了。最开始真的html都不会。。。。。。然后到现在两个月多了,不好意思才看到第六章第七章。。。