上传文件并提交

时间:2019-01-08 18:39:04

标签: python flask sijax

尝试使用flask-sijax上传示例上传文件然后提交,但是由于呈现了upload.html,因此提交按钮不断被删除。解决这个问题的最佳方法是什么?

当前,需要单击两次“上传”按钮才能运行,然后代码将生成“提交”按钮,但是当单击“提交”按钮时,它将呈现upload.html,从而导致删除了“提交”按钮

upload.py

Quiz

upload.html

import os, sys
path = os.path.join('.', os.path.dirname(__file__), '../')
sys.path.append(path)
from flask import Flask, g, render_template, request
import flask_sijax
app = Flask(__name__)
app.config["SIJAX_STATIC_PATH"] = os.path.join('.', 
os.path.dirname(__file__), 'static/js/sijax/')
app.config["SIJAX_JSON_URI"] = '/static/js/sijax/json2.js'
flask_sijax.Sijax(app)
class SijaxHandler(object):
    @staticmethod
    def form_one_handler(obj_response, files, form_values):
        print("one")
        obj_response.html_append('#formOneResponse', """
 <div style="float: left; width: 450px">
        <form id="formTwo" name="formTwo" style="width: 400px" 
 method="post" enctype="multipart/form-data">
            Everything checks out

            <input type="submit" value="submit" name="two"/>
        </form>

        <div id="formTwoResponse"></div>
    </div>
 """)
    @staticmethod
    def form_two_handler(obj_response, files, form_values):
        print("two")
        obj_response.html_append('#formTwoResponse', '<br />Finished!')
@flask_sijax.route(app, "/", methods=['GET','POST'])
def index():
    form_init_js = ''
    if request.method =='POST':
        if request.form.get('one') == 'Upload':
            print("upload")
            form_init_js += g.sijax.register_upload_callback('formOne', SijaxHandler.form_one_handler)
        elif request.form.get('two') == 'submit':
            print("submit")
            form_init_js += g.sijax.register_upload_callback('formTwo', SijaxHandler.form_two_handler)
    if g.sijax.is_sijax_request:
        # The request looks like a valid Sijax request
        # The handlers are already registered above.. we can process the request
        return g.sijax.process_request()
    return render_template('upload.html', form_init_js=form_init_js)

if __name__ == '__main__':
    app.run(debug=True, port=8080)

一键尝试使“上传”按钮起作用,而使提交按钮停留在页面上。

0 个答案:

没有答案