如何将表单数据和上传的图像显示到烧瓶中的页面

how is it possible to show form data and uploaded image to a page in flask

如何在页面上显示表单数据和上传的图片。就像它是一个学生申请网站,学生将在其中填写他的信息和他的照片。所以当他提交时,他将被重定向到另一个页面 "apply.html",其中填写了他自己的信息,页面上有一张图片,有点像录取卡。

我可以将图片上传到我的 "static/uploads" 文件夹,而且当提交表单时,用户会被重定向到 "apply.html" 并填写了信息,但我找不到显示图片的方法在 "apply.html"

这是我的代码

@app.route('/form.html', methods=['GET', 'POST'])
def form():
  nform = NewRegistration(request.form)
  if request.method == 'POST':
    if nform.validate() == False:
      flash('All fields are required.')
      return render_template('form.html', form=nform)
    else:

        try:
            file = request.files['file']
            if file and allowed_file(file.filename):
                filename = secure_filename(file.filename)
                file.save(os.path.join(app.config['UPLOAD_FOLDER'], filename))
        except Exception as e:
            print "Form without file "+e
        post = request.form['element_15'].strip()
        name = request.form['element_1_1'].strip()
        last = request.form['element_1_2'].strip()
        Name = str(name)+ ' ' +str(last)
        father = request.form['element_2'].strip()
        mother = request.form['element_3'].strip()
        gender = request.form['element_17'].strip()
        data = {'Name' : Name, 'post' : post, 'father' : father}
        return render_template("apply.html", data=data)

    elif request.method == 'GET':
      return render_template('form.html', form=nform)

在您的 apply.html 模板中,您可以有一个 <img> 标签指向您上传的图片。考虑将图像保存在 img 文件夹下的 static 文件夹中。然后你可以使用:

<img src="{{url_for('static', filename='img/' + filename)}}">

为了使用文件名添加一个文件名变量到你的表单视图return语句:

return render_template("apply.html", data=data, filename=filename)