向我在 flask 上运行的网站提交空白表单会导致 return 内部服务器错误

Submitting a blank form to my website running on flask makes it return an internal server error

我在 NumbersIndia.com . When I try to submit a balnk form the form at http://numbersindia.com/flask_hello_world.fcgi/track_mobile_numbers 的 Flask 中运行我的网络服务器,或者我得到一个内部服务器的 pincode 形式。 这是密码路线的代码。

@app.route('/find_pin_codes', methods=['POST','GET'])
def pincodes():
    if request.method == 'POST':
        if request.form["number"] != "":
            number = request.form['number']
            number = number.replace(" ","")
            number = number.replace("-","")
            number = number.strip()
            strnumber = number
            try:
                number = int(number)
                num = pinCodes.query.filter_by(pincode = number).first()
                return render_template('pincodes.html', pincode = "Pincode : "+strnumber, 
                                    officename = "Office name : "+num.officename,  
                                    taluk = "Taluk : "+num.taluk, 
                                    district ="District : "+ num.district,
                                    state = "State : "+num.state
                                    )
            except:
                return render_template('pincodes.html', 
                                    pincode = "Pincode : "+strnumber, 
                                    officename = "Invalid pincode, try again")
        else:
            return render_template('pincodes.html', 
                                    pincode = "Pincode : "+strnumber, 
                                    officename = "Invalid pincode, try again")

    else:
        return render_template('pincodes.html')

当您提交空白表单时,尝试访问 request.form["number"] 失败并返回 KeyError,因为字典 request.form 没有关键字 "number"。你可以通过改变

来解决这个问题
if request.form["number"] != "":

if request.form.get("number", "") != "":