使用flask和python从html表单获取表单数据 [英] Getting form data from html form using flask and python

查看:2769
本文介绍了使用flask和python从html表单获取表单数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当按下提交按钮时,我试图从表单数据中取出表单数据,所以我可以把它放入json格式,并以另一个页面的形式访问json数据,这个页面应该是 localhost:5000 /信息。每次我尝试使用 request.form.get('< id>)访问数据时,它只返回一个空字典。我读了stackoverflow上的其他职位试图找出问题,但没有解决方案似乎工作。如果可能的话,我想避免使用模板或模块以外的瓶子。

这是我的Python代码



< pre $ 从瓶子导入烧瓶,请求,jsonify,重定向

app =烧瓶(__ name__)

numCarsEast =无
numCarsWest = None
numCarsSouth = None
numCarsNorth = None


@ app.route('/ info.json',methods = ['GET','POST '))
def getInfo():$ b $如果request.method =='GET':
lightEast = {}
lightWest = {}
lightNorth = {}
lightSouth = {}
intersection1 = {}
lightEast ['cars'] = numCarsEast
lightWest ['cars'] = numCarsWest
lightNorth ['cars'] = numCarsNorth
lightSouth ['cars'] = numCarsSouth
intersection1 ['eastLight'] = lightEast
intersection1 ['westLight'] = lightWest
intersection1 ['northLight'] = lightNorth
intersection1 ['southLight'] = lightSouth
返回jsonify(intersection = intersection1)

@ app.route('/ cars',methods = ['GET','POST '))
def cars():
if request.method =='POST':
numCarsEast = request.form.get('eastLightInt1',None)
numCarsWest = request.form.get('westLightInt1',None)
numCarsNorth = request.form.get('northLightInt1',None)
numCarsSouth = request.form.get('southLightInt1',None)
print(str(numCarsNast)+'north')
print(str(numCarsWest)+'west')
print(str(numCarsNorth)+'north')
print (numCarsSouth)+'south')
return'done'
return open('./carForm.html')。read()
$ b $ if if __name__ =='__main__' :
app.debug = True
app.run()

HTML

 < body> 
< form method ='POST'>
< H2>
十字路口1

< label>
East Light
< / label>
< input type = text id ='eastLightInt1'name ='eastLightInt1'/>
< label>
West Light
< / label>
< input type = text id ='westLightInt1'name ='westLightInt1'/>
< br />
< label>
北极光
< / label>
< input type = text id ='northLightInt1'name ='northLightInt1'/>
< label>
南灯
< / label>
< input type = text id ='southLightInt1'name ='southLightInt1'/>
< br />
< input type = submit value = Submit>
< / form>
< / body>


解决方案

我试过你的代码,它适用于我。 / p>

我只需在 cars()中添加 global 在 /info.json


$ b $

  def cars() :
global numCarsEast,numCarsWest,numCarsSouth,numCarsNorth


I am trying to get the form data out of the text fields when the submit is pressed so I can put it into json format and access the json data as another page which would be localhost:5000/info. Every time I try to access the data with request.form.get('<id>') it only returns an empty dictionary. I read the other posts on stackoverflow trying to figure out the problem but none of the solutions seem to work. If possible I would like to avoid having to use templates or modules other than flask.

This is my python code

from flask import Flask, request, jsonify, redirect

app = Flask(__name__)

numCarsEast = None
numCarsWest = None
numCarsSouth = None
numCarsNorth = None


@app.route('/info.json', methods=['GET', 'POST'])
def getInfo():
    if  request.method == 'GET':
        lightEast = {}
        lightWest = {}
        lightNorth = {}
        lightSouth = {}
        intersection1 = {}
        lightEast['cars'] = numCarsEast
        lightWest['cars'] = numCarsWest
        lightNorth['cars'] = numCarsNorth
        lightSouth['cars'] = numCarsSouth
        intersection1['eastLight'] = lightEast
        intersection1['westLight'] = lightWest
        intersection1['northLight'] = lightNorth
        intersection1['southLight'] = lightSouth
        return jsonify(intersection=intersection1)

@app.route('/cars', methods=['GET', 'POST'])
def cars():
    if request.method == 'POST':
        numCarsEast = request.form.get('eastLightInt1', None)
        numCarsWest = request.form.get('westLightInt1', None)
        numCarsNorth = request.form.get('northLightInt1', None)
        numCarsSouth = request.form.get('southLightInt1', None)
        print(str(numCarsEast) + ' east')
        print(str(numCarsWest) + ' west')
        print(str(numCarsNorth) + ' north')
        print(str(numCarsSouth) + ' south')
        return 'done'
    return open('./carForm.html').read()

if __name__ == '__main__':
    app.debug = True
    app.run()

This is the HTML

<body>
    <form method='POST'>
        <H2>
            Intersection 1
        </h2>
        <label>
            East Light
        </label>
        <input type=text id='eastLightInt1' name='eastLightInt1' />
        <label>
            West Light
        </label>
    <input type=text id='westLightInt1' name='westLightInt1' />
    <br />
    <label>
      North Light
    </label>
    <input type=text id='northLightInt1' name='northLightInt1' />
    <label>
      South Light
    </label>
    <input type=text id='southLightInt1' name='southLightInt1' />
    <br />
    <input type=submit value=Submit>
  </form>
  </body>

解决方案

I tried your code and it works for me.

I only had to add global in cars() to get results on page /info.json

def cars():
    global numCarsEast, numCarsWest, numCarsSouth, numCarsNorth

这篇关于使用flask和python从html表单获取表单数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆