相同格式的HTML按钮无法正常工作

时间:2019-06-21 13:07:40

标签: python html flask

我有以下HTML代码:

<html lang="en">

    <h1>Interconnector Schedules</h1>


<form class="form-inline" method="POST" action="/submit">
  <div class="form-group">
    <div class="input-group">

        {% if files %}
        <select name="Schedules" size="6">
        {% for file in files %}
            <option value="{{file}}" SELECTED>{{ file }}</option>
        {% endfor %}
        {% endif %}
        </select>
    </div>
    <button type="submit" class="btn btn-default" formaction="{{ 
url_for('load_file') }}">Load Schedule</button>
    <button type="submit" class="btn btn-default" formaction="{{ 
url_for('del_file') }}">Delete Schedule</button>
  </div>
</form>

</html>

我遇到的问题是两个按钮都从我的Python脚本中执行load_file方法,这对第一个按钮很好,但是我无法让第二个按钮在Python中调用del_file函数。

有人可以帮忙吗?

Python代码-server.py包含网址定义

def create_app():
    app = Flask(__name__)


    app.add_url_rule("/", view_func=views.home_page)
    app.add_url_rule("/Interconnector_Schedules", 
view_func=views.schedule_page)
    app.add_url_rule("/Interconnector_Schedules", methods=['GET', 'POST'], 
view_func=views.load_file)
    app.add_url_rule("/Interconnector_Schedules", methods=['GET', 'POST'], 
view_func=views.del_file)

    return app


if __name__ == "__main__":
    app = create_app()
    port = int(os.environ.get('PORT', 5000))
    app.run(host='0.0.0.0', port=port)

Python代码:views.py包含要在html页面上使用的函数

def schedule_page():
    files = os.listdir(abs_path)
    return render_template("schedules.html", files=sorted(files))

def load_file():
    filename = request.form.get('Schedules')

    if filename.startswith('RESPONSE'):
        shutil.copy(abs_path + filename, temp + 'RESPONSE.csv')
        shutil.move(temp + 'RESPONSE.csv', m + 'RESPONSE.csv')
        select = (filename + ' file has been loaded successfully')

    elif 'EMSRP' in filename:
        shutil.copy(abs_path + filename, temp + filename[:-7])
        shutil.move(temp + filename[:-7], e + filename[:-7])
        select = (filename + ' file has been loaded successfully')

    else:

        select = filename + ' file is not a schedule file, please return to 
the Interconnector Schedules page and select an appropriate schedule file.'

    return render_template("return.html", message = select)

def del_file():
    filename = request.form.get('Schedules')
    os.remove(abs_path + filename)
    select = filename + ' file has been deleted.'

    return render_template("return.html", message = select)

0 个答案:

没有答案