Reputation: 5
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
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>
Upvotes: 0
Views: 1693
Reputation: 931
There is nothing in your models.py file. I believe you are doing it totally the wrong way. You should create tables by Django models not directly by running create table commands in MYSQL shell, like:
from django.db import models
class Area(models.Model):
name = models.CharField(max_length=30)
def __str__(self):
return self.name
class Zone(models.Model):
Area = models.ForeignKey(Area, on_delete=models.CASCADE)
name = models.CharField(max_length=30)
def __str__(self):
return self.name
class Rack(models.Model):
Zone = models.ForeignKey(Zone, on_delete=models.CASCADE)
name = models.CharField(max_length=30)
def __str__(self):
return self.name
Moreover, doing a direct MySQL connection is not a good practice in Django as Django automatically handles database connections see here: https://docs.djangoproject.com/en/3.0/topics/db/models/#module-django.db.models
On the top of everything that you are trying to do is best exampled in here: https://simpleisbetterthancomplex.com/tutorial/2018/01/29/how-to-implement-dependent-or-chained-dropdown-list-with-django.html you can follow this blog and try to interdependent dropdowns in Django
Upvotes: 1