mysterious_guy mysterious_guy - 11 months ago 129
Python Question

Connecting HTML drop down list to Flask

Below is my HTML page:

<form action = "{{url_for('enter_ID')}}" method = "POST">
<select name = "ID">
<option value="ID1">Galactic_ID</option>
<option value="ID2">Phone_ID</option>
<option value="ID3">ident_ID</option>
ID:<input type = "text" name = "ID" />
<input type='submit' value='Submit'>

below is my

from models1 import Customer,Phone,ID_Card
from flask import Flask, request, session, redirect, url_for, render_template, flash,json,jsonify
import os
app = Flask(__name__)
@app.route('/',methods = ['GET','POST'])
def enter_ID():
if request.method == 'POST':
Galactic_ID = request.form['ID']
if Customer(Galactic_ID).find():
data = Customer(Galactic_ID).Customer_relationships()
return render_template('dropdown.html',data=json.dumps(data))
Phone_ID = request.form['ID']
if Phone(Phone_ID).find():
data = Phone(Phone_ID).Phone_relationships()
return render_template('dropdown.html',data=json.dumps(data))
ident_ID = request.form['ID']
if ID_Card(ident_ID).find():
data = ID_Card(ident_ID).ID_Card_relationships()
return render_template('dropdown.html',data=json.dumps(data))

return render_template('dropdown.html')

I get the error:

ValueError: View function did not return a response

My dropdown.html does have the necessary CSS and other details needed to render a d3.js graph. I have not found a standard way to connect the dropdown list to Flask. So , thought of posting it here.

Answer Source

If the request method is POST, but none of the filters trigger, no return statement is reached, and so None is returned, which is not a valid response.

Remove else before the last render_template.