表单提交后如何获取新对象?
How to get a new object after form submittig?
我正在尝试使用 flask,但有一个提交表单的简单任务。
页面显示一张图片和一张表格,如果表格提交正确,图片应该更改,如果不正确 - 是一样的。
我无法理解如何在页面上只显示一个对象并在提交表单后获取另一个对象的机制。
尝试在图像列表上使用迭代器是文件夹 "static",但我的实现工作不正常。
请给我反馈如何以正确的方式做到这一点?
现在我有了简单的看法:
@app.route("/", methods=["GET", "POST"])
def start_view():
picture = None
form = InputForm(csrf_enabled=False)
if form.validate_on_submit():
picture = form.picture.data
form.picture.data = ""
return render_template('04-2.html', form=form, picture=picture)
class InputForm(Form):
picture = StringField('What is on a picture?', validators[DataRequired()])
submit = SubmitField('Submit')
还有一个简单的模板:
<body>
<form method="POST">
{{ form.picture.label }} {{ form.picture }}
{{ form.submit() }}
</form>
{% if form.errors %}
<span style="color: red">{{ form.error }}</span>
{% endif %}
</body>
谢谢!
您的表单不包含任何图片。它有一个 StringField
和一个 SubmitField
。如果您想查看任何图像,您需要在 HTML 中有一个 <img>
标记,指向服务器中的图像位置
您的视图应如下所示:
from Flask import session
# in order to use sessions you have to use a secret key for your app
app.secret_key = 'some secret key'
@app.route("/", methods=["GET", "POST"])
def start_view():
img_list = ['filename1', 'filename2', 'filename3']
# if this is not the first form submission
if session.has_key('current'):
# if we reach the end of the list show the first image again
if int(session['current']) == len(img_list) - 1:
session['current'] = 0
# move to next image
else:
session['current'] = int(session['current']) + 1
else:
session['current'] = 0
picture = 'first_image_filename' # this should be the img on load
form = InputForm(csrf_enabled=False)
if form.validate_on_submit():
picture = img_list[int(session['current'])] # the filename of the next image
form.picture.data = ""
return render_template('04-2.html', form=form, picture=picture)
因此模板应如下所示:
<body>
<form method="POST">
{{ form.picture.label }} {{ form.picture }}
<img src="{{url_for('static', filename='img/' + picture)}}"
{{ form.submit() }}
</form>
{% if form.errors %}
<span style="color: red">{{ form.error }}</span>
{% endif %}
</body>
我正在尝试使用 flask,但有一个提交表单的简单任务。 页面显示一张图片和一张表格,如果表格提交正确,图片应该更改,如果不正确 - 是一样的。 我无法理解如何在页面上只显示一个对象并在提交表单后获取另一个对象的机制。 尝试在图像列表上使用迭代器是文件夹 "static",但我的实现工作不正常。 请给我反馈如何以正确的方式做到这一点?
现在我有了简单的看法:
@app.route("/", methods=["GET", "POST"])
def start_view():
picture = None
form = InputForm(csrf_enabled=False)
if form.validate_on_submit():
picture = form.picture.data
form.picture.data = ""
return render_template('04-2.html', form=form, picture=picture)
class InputForm(Form):
picture = StringField('What is on a picture?', validators[DataRequired()])
submit = SubmitField('Submit')
还有一个简单的模板:
<body>
<form method="POST">
{{ form.picture.label }} {{ form.picture }}
{{ form.submit() }}
</form>
{% if form.errors %}
<span style="color: red">{{ form.error }}</span>
{% endif %}
</body>
谢谢!
您的表单不包含任何图片。它有一个 StringField
和一个 SubmitField
。如果您想查看任何图像,您需要在 HTML 中有一个 <img>
标记,指向服务器中的图像位置
您的视图应如下所示:
from Flask import session
# in order to use sessions you have to use a secret key for your app
app.secret_key = 'some secret key'
@app.route("/", methods=["GET", "POST"])
def start_view():
img_list = ['filename1', 'filename2', 'filename3']
# if this is not the first form submission
if session.has_key('current'):
# if we reach the end of the list show the first image again
if int(session['current']) == len(img_list) - 1:
session['current'] = 0
# move to next image
else:
session['current'] = int(session['current']) + 1
else:
session['current'] = 0
picture = 'first_image_filename' # this should be the img on load
form = InputForm(csrf_enabled=False)
if form.validate_on_submit():
picture = img_list[int(session['current'])] # the filename of the next image
form.picture.data = ""
return render_template('04-2.html', form=form, picture=picture)
因此模板应如下所示:
<body>
<form method="POST">
{{ form.picture.label }} {{ form.picture }}
<img src="{{url_for('static', filename='img/' + picture)}}"
{{ form.submit() }}
</form>
{% if form.errors %}
<span style="color: red">{{ form.error }}</span>
{% endif %}
</body>