Sending data from HTML form to a Python script in Flask

后端 未结 2 952
一个人的身影
一个人的身影 2020-11-22 03:18

I have the code below in my Python script:

def cmd_wui(argv, path_to_tx):
    \"\"\"Run a web UI.\"\"\"
    from flask import Flask, flash, jsonify, render_t         


        
2条回答
  •  天命终不由人
    2020-11-22 03:51

    The form tag needs two attributes set:

    1. action: The URL that the form data is sent to on submit. Generate it with url_for. It can be omitted if the same URL handles showing the form and processing the data.
    2. method: Whether to submit the data as a query string (GET) or form data (POST).

    The input tag needs a name parameter.

    Add a view to handle the submitted data, which is in request.form under the same key as the input's name.

    @app.route('/handle_data', methods=['POST'])
    def handle_data():
        projectpath = request.form['projectFilepath']
        # your code
        # return a response
    

    Set the form's action to that view's URL using url_for:

提交回复
热议问题