为什么程序返回一个空响应

huangapple go评论125阅读模式
英文:

Why does the program return an empty response

问题

这是我的FLASK代码。我尝试在flask库中创建验证表单,也尝试使用Postman请求进行检查,但它不起作用,请帮助:

from flask import Flask
from flask_wtf import FlaskForm
from wtforms import StringField, IntegerField

app = Flask(__name__)
app.config['WTF_CSRF_ENABLED'] = False

class RegistrationForm(FlaskForm):
    email = StringField()
    phone = IntegerField()
    name = StringField()
    address = StringField()
    index = IntegerField()
    comment = StringField()

@app.route('/registration', methods=['POST'])
def registration():
    form = RegistrationForm()
    if form.validate_on_submit():
        email, phone = form.email.data, form.phone.data
        return f"Successfully registered user {email} with phone +7{phone}"
    return f'Invalid input, {form.errors}', 400

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

我尝试使用Postman进行POST请求,但它返回:Successfully registered user None with phone +7None。

我该如何修复这个问题,请帮助。

尝试重新编写代码,进行调试,但没有找到异常。

英文:

This is my code in FLASK. I try to make validation form in flask library. also i try to check it with postman request. But it doesn't work, please help:

from flask import Flask
from flask_wtf import FlaskForm
from wtforms import StringField, IntegerField

app = Flask(__name__)
app.config['WTF_CSRF_ENABLED'] = False

class RegistrationForm(FlaskForm):
    email = StringField()
    phone = IntegerField()
    name = StringField()
    address = StringField()
    index = IntegerField()
    comment = StringField()


@app.route('/registration', methods=['POST'])
def registration():
    form = RegistrationForm()
    if form.validate_on_submit():
        email, phone = form.email.data, form.phone.data
        return fr"Successfully registered user {email} with phone +7{phone}"
    return f'Invalid input, {form.errors}', 400


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

I try to make POST with postman, but it return: Successfully registered user None with phone +7None.

How can i fix that, please, help

Try to rewrite code, try to debug, but i didn't find that exception

答案1

得分: 1

不必将"request.form"对象传递给RegistrationForm()吗?

我总是看到这样做:

将以下代码:

form = RegistrationForm()

替换为:

form = RegistrationForm(request.form)

您还需要更新:

from flask import Flask

为:

from flask import Flask, request

如果这不起作用,请继续下一个调试步骤:

某些原因导致form变量包含的内容与您预期的不同

尝试这个并报告输出结果(请在问题中更新,而不要在评论中提供答案,因为您可以在问题中正确格式化它,而在评论中无法)。

在这两行之间添加一行。将这个:

if form.validate_on_submit():
    email, phone = form.email.data, form.phone.data

更改为:

if form.validate_on_submit():
    print(form)
    email, phone = form.email.data, form.phone.data
英文:

Don't you have to pass the "request.form" object into the RegistrationForm() ?

I have always seen it done like this:

Replace:

form = RegistrationForm()

with:

form = RegistrationForm(request.form) 

You will also need to update

from flask import Flask

to:

from flask import Flask,request

If that doesn't work, go forward to the next debugging step:

Something is preventing the form variable from containing what you expect it to contain

Try this and report what it outputs. (Update your question, rather than putting the answer here, because you can correctly format it in the question, but not in a comment).

Add one line between these two lines. Change this:

    if form.validate_on_submit():
        email, phone = form.email.data, form.phone.data

... to this:

    if form.validate_on_submit():
        print(form)
        email, phone = form.email.data, form.phone.data

huangapple
  • 本文由 发表于 2023年3月7日 13:32:01
  • 转载请务必保留本文链接:https://go.coder-hub.com/75658340.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定