0

I am working on a flask app that will take a user input from a form and pass it on to the Todosit API to generate a task. I have been able to authenticate and verify the API version of the code is working, my only hangup now seems to be getting the data from the front end (jinja template) passed in as a variable to the Todoist API POST call.

Here is my Python code for the route:

@app.route('/', methods=['GET', 'POST'])
def home():
    if request.form["submit"] == "submit":
        task = request.form["task"]
        due_date = request.form["dueDate"]
        
        requests.post(
        "https://api.todoist.com/rest/v1/tasks",
        data=json.dumps({
            "content": task,
            "label_ids": [2154915513],
            "due_string": due_date,
            "due_lang": "en",
            "priority": 1
        }),
        headers={
            "Content-Type": "application/json",
            "X-Request-Id": str(uuid.uuid4()),
            "Authorization": f"Bearer {API_KEY}"
        }).json()

Here is the HTML form code:

<form>
        <label for="task">Enter Task:</label><br>
        <input type="text" id="task" name="task"><br>
        <label for="due_date">Enter Due Date (Leave Blank if None):</label><br>
        <input type="text" id="due_date" name="due_date">
        <input type="submit" value="Submit">
    </form>

When I run this and add a task & due date, the results are not passed into the variables task & due date correctly. Does anyone have any advice on passing front end variables to the Flask route for processing? Thanks!

1 Answers1

0

Add for form tag: action="{{ url_for('handle_data') }}" method="post" And for dueDate use right key: request.form["due_date"] Hope its help

Mikhail Vitik
  • 506
  • 7
  • 10
  • Thanks for the quick response Mike. Just to make sure I understand correctly, the url_for('handle_data') should lead to a route in application.py for handle_data, correct? – Matt Sunner Jul 24 '20 at 13:55
  • Yes. You can see more info [here](https://stackoverflow.com/questions/11556958/sending-data-from-html-form-to-a-python-script-in-flask) – Mikhail Vitik Jul 27 '20 at 09:50