-1

app.py:

from flask import Flask, flash, redirect, render_template, request, url_for
app = Flask(__name__)
import requests
import json

@app.route("/", methods=['GET'])
def fill_devices():
    devices = requests.request("GET", "http://10.64.127.94:5000/api/get_platforms", headers={}, data="").json()["final_result"]
    return render_template('devices.html', devices=devices)

@app.route('/submit_device', methods = ['POST'])
def submit_device():
    # get the device name here
    labels = requests.request("GET", "http://10.64.127.94:5000/api/get_labels", headers={},
              data=json.dumps(
                  { "device": ""}
              )).json()["final_result"]
    return render_template('labels.html', labels=labels)

if __name__ == "__main__":
    app.run(host='127.0.0.1', debug=True, port=5000)

labels.html

{% extends "index.html" %}
{% block devices %}
        <select name="device_id">
            <option>SELECT</option>
            {% for device in devices %}
            <option>{{ device }}</option>
            {% endfor %}
        </select>
{% endblock devices %}

When someone changes the value in drop down, I should get that in app.py so that I can fill the other drop down and render. How to achieve this?

E_net4
  • 27,810
  • 13
  • 101
  • 139
Akshay J
  • 5,362
  • 13
  • 68
  • 105
  • You could simply use `request.form.get` function for getting post form-data in the handler function. Getting device_id like `request.form.get('device_id')` should work. – vikas soni Feb 23 '23 at 17:12

1 Answers1

0

You'd also need to import request from flask library for getting request body. use request.form.get("<form-field-name>") for getting form field.

from flask import Flask, request, flash, redirect, render_template, request, url_for
app = Flask(__name__)
import requests
import json

@app.route("/", methods=['GET'])
def fill_devices():
    devices = requests.request("GET", "http://10.64.127.94:5000/api/get_platforms", headers={}, data="").json()["final_result"]
    return render_template('devices.html', devices=devices)

@app.route('/submit_device', methods = ['POST'])
def submit_device():
    # get the device name here
    labels = requests.request("GET", "http://10.64.127.94:5000/api/get_labels", headers={},
              data=json.dumps(
                  { "device": request.form.get("device_id")}
              )).json()["final_result"]
    return render_template('labels.html', labels=labels)

if __name__ == "__main__":
    app.run(host='127.0.0.1', debug=True, port=5000)
vikas soni
  • 540
  • 3
  • 9
  • Thanks, I tried this but the request itself is not coming to the flask route when I change something in the first drop down @app.route('/submit_device', methods = ['POST']) – Akshay J Feb 24 '23 at 05:11