【问题标题】:HOw to send data and file from a form using flask?如何使用烧瓶从表单发送数据和文件?
【发布时间】:2015-10-20 11:01:04
【问题描述】:

我使用 html 和烧瓶创建了一个表单。用户将在这里填写他的姓名、地址和其他信息,还将上传他的照片和其他文件。一旦用户填写信息并提交表格,他将被重定向到另一个页面,页面上有他自己填写的信息和照片。

我能够填写用户信息并将他重定向到另一个页面“apply.html”,但是当我尝试上传照片时。它能够上传图片,但不会将我重定向到“apply.html”

在我的 routes.py 中

def allowed_file(filename):
    return '.' in filename and \
           filename.rsplit('.', 1)[1] in ALLOWED_EXTENSIONS


@app.route('/form.html', methods=['POST'])
def upload_file():
    nform = NewRegistration(request.form)
    if request.method == 'POST':
        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))


@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:
      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)

我知道问题是由于两个函数“upload_file”和“form”所以建议我获取信息和照片的最佳方法,并且还能够将用户重定向到 apply.html

【问题讨论】:

    标签: flask flask-sqlalchemy flask-wtforms flask-login


    【解决方案1】:

    因为需要在

    中添加render_template()
    @app.route('/form.html', methods=['POST'])
    def upload_file():
        // do something
        render_template("yourpage.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)
    

    如果这有帮助,请告诉我。

    【讨论】:

    • 我以前也这样做过。我都试过了。你告诉它显示““POST /form.html HTTP/1.1”200 表示表单正在发布。但它没有保存图像,也没有返回到 apply.html。它继续让我再次回到同一页面。 .
    • 好吧,今天对我来说同样有效。我猜,谢谢。你能告诉我如何将表单数据和上传的图像显示到页面上吗?就像它是一个学生申请网站,学生将在其中填写他的信息以及他的照片。所以当他提交时,他将被重定向到另一个页面,其中填写了他自己的信息并在页面上显示了一张图片,有点像准考证。
    • 现在使用 jinja 模板来解析你通过 render_template( "apply.html" , data=data ) 传递的数据。你的 apply.html 应该是这样的:{% if data %} Name : {{ data['Name'] }} Father : {{ data['father'] }} {% endif %} 你在 render_template("apply.html" , data=data) 上传递的数据越多,同样可以在页面上呈现。
    猜你喜欢
    • 2014-07-16
    • 1970-01-01
    • 1970-01-01
    • 2021-08-19
    • 1970-01-01
    • 1970-01-01
    • 2019-05-13
    • 1970-01-01
    • 2017-12-26
    相关资源
    最近更新 更多