i have two drop downs from the mysql database in which both are interdependent. what i need is when i select an area from the first drop down, the second drop down as to filter accordingly matching the area from the first drop down. can anyone help me out. [The first drop down is area and the second drop down is zone ].kindly help me out with the changes in this two files. thanks in advance.
models.py
from django.db import models
Create your models here.
views.py
def dashboard_view(request): import mysql.connector
connection = mysql.connector.connect(host='localhost',
database='lab_view',
user='root',
password='inbabtslabuser'
)
sql_select_Query ="""select DISTINCT Area from main_labview;"""
cursor = connection.cursor()
cursor.execute(sql_select_Query)
records = cursor.fetchall()
sql_select_Query ="""select DISTINCT Zone from main_labview;"""
cursor = connection.cursor()
cursor.execute(sql_select_Query)
records1 = cursor.fetchall()
sql_select_Query ="""select DISTINCT Rack from main_labview;"""
cursor = connection.cursor()
cursor.execute(sql_select_Query)
records2 = cursor.fetchall()
a_list=[]
z_list=[]
r_list=[]
for row in records2:
r_list.append(row[0])
for row in records:
a_list.append(row[0])
for row in records1:
z_list.append(row[0])
print("\n\n###########################################\nFrom Database:\nAreas: ", a_list)
print("Zones: ",z_list)
print("###########################################\n\n")
context={'Area':a_list, 'Zones':z_list, 'Racks':r_list}
return render(request, 'app/dashboard.html', context)
.html
<form action="/db2/" method="POST"> {% csrf_token %}
<strong><font color="red">Area:</font></strong>
<select id="Area_ID" name="Area" >
<option value="None" selected>Select Area</option>
{% for a in Area %}
<option value="{{ a }}">{{ a }}</option>
{% endfor %}
</select>
<br><br>
<strong><font color="red" >Zone:</font></strong>
<select id="Zone_ID" name="Zone" >
<option value="None" selected>Select Zone</option>
{% for z in Zones %}
<option value="{{ z }}">{{ z }}</option>
{% endfor %}
</select>
<br> </br>
<button type="submit" name="try" onClick="location.href='{% url 'db2' %}';" value='submits'>Find Racks</button><br>
</form>