I am new to Django and struggling to figure out how to query a database and then return a filtered list based off of drop-down selections. I have tried doing it with forms but I'm not sure this is necessary as I do not need to store the drop-down data- I just want to use it to query a database and filter it accordingly. So if user selects "dog" in the "type" drop-down I would then query the Dog db and then filter it based off of the second drop-down selection of "age_group". So, if user selected "dog" and then "baby" it would return a list of puppies. dog.model
class Dog(models.Model):
name =models.CharField(max_length=200,validators=[MinLengthValidator(2, "Nickname must
be greater than 1 character")])
breed_group = models.CharField(max_length=200, choices=BreedGroup.choices, null=False,
blank=False, default='Not Enough Information')
breeds = models.ForeignKey(DogBreed, on_delete=models.PROTECT)
age = models.PositiveIntegerField()
ageGroup = models.IntegerField(choices=AgeGroup.choices, null=False, blank=False,
default=0)
sex = models.IntegerField(choices=SEX, blank=False, null=False, default=0)
tagLine = models.CharField(max_length=150)
goodWithCats = models.BooleanField(blank=False, null=False, default='Not Enough
Information')
goodWithDogs = models.BooleanField(null=False, blank=False, default='Not Enough
Information')
goodWKids = models.BooleanField(null=False, blank=False, default='Not Enough
Information')
def __str__(self):
return self.name
search.html
<div class="row" >
<div class="col-md-12 justify-content-sm-center">
<div class="input-group" id="searchBox">
<div class="col-xs-1 mx-4"></div>
<form class="form-inline" action="." method="GET">
<div class = "justify-content-sm-center">
<label class="sr-only type " for="type" >TYPE</label>
<select class="custom-select my-1 mr-sm-2" id="type2">
<option selected>ALL TYPES</option> # queries all
<option value="1">CAT</option> # queries class CAT
<option value="2">DOG</option> # queries class Dog
<option value="3">OTHER</option> # queries class Other
</select>
</div>
<div class = "justify-content-sm-center"></div>
<label class="sr-only agelabel " for="age">AGE</label>
<select class="custom-select my-1 mr-sm-2" id="agelabel2">
<option selected>ANY AGE</option> # returns all listings
<option value="1">BABY</option> # returns babies of selected
type
<option value="2">ADULT</option> #'' adults ''
<option value="3">SENIOR</option>#'' seniors ''
</select>
<button type="submit" class="btn btn-primary mb-2">SEARCH</button>
</form>
</div>
</div>
</div>