2015-10-20 87 views
-1

我已經使用html和燒瓶創建了一個表單。用戶將填寫他的姓名,地址和其他信息,並且還會上傳他的照片和其他文件。一旦用戶填寫信息並提交表格,他將被重定向到另一個頁面,並在頁面上顯示他自己填充的信息和照片。如何使用燒瓶從窗體發送數據和文件?

我能夠獲取用戶信息並將其重定向到另一個頁面「apply.html」,但是當我嘗試上傳照片時。它能夠上傳PIC,但不重定向我在我的routes.py「apply.html」

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」和「形式」,因此建議我得到的信息和照片,也可以重定向用戶apply.html

回答

1

監守你需要在

@app.route('/form.html', methods=['POST']) 
def upload_file(): 
    // do something 
    render_template("yourpage.html") 

添加render_template()每個路徑必須返回一個重新的最佳方式sponse。 另外,我會建議使用相同的路線來保存文件+表單。

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

讓我知道這是否有幫助。

+0

我之前做過同樣的事情。我嘗試了所有。你告訴它顯示「」POST /form.html HTTP/1.1「200意味着表單正在發佈,但它沒有保存圖像,也沒有返回到apply.html。 它不斷返回到同一頁面再次。 – ravenwingz

+0

你能告訴我你的完整的燒瓶代碼和html。 – abhishekrana

+0

確定同樣的工作,對我來說,今天我想和謝謝你,你能告訴我如何能夠顯示錶單數據和上傳圖像到一個頁面。這是一個學生申請網站,學生將填寫他的信息和他的照片,所以當他提交時,他將被重定向到另一個頁面,他自己填寫的信息和頁面上的圖像,類型的錄取卡 – ravenwingz