英文:
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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论