Issue
I'm trying to filter category with age range. I want to see people with certain genders and then their age range.
My models:
from django.db import models
# Create your models here.
class catagory(models.Model):
name = models.CharField(max_length=150)
def __str__(self):
return self.name
class Age(models.Model):
range = models.CharField(max_length=150)
def __str__(self):
return self.range
class person(models.Model):
name = models.CharField(max_length=100)
gender = models.ForeignKey(catagory, on_delete=models.CASCADE)
age_range = models.ForeignKey(Age, on_delete=models.CASCADE)
@staticmethod
def get_person_by_cataegoryID(categoryID):
if categoryID:
return person.objects.filter(gender=categoryID)
else:
return person.objects.all()
@staticmethod
def get_person_by_age_range(AGE_RANGE):
if AGE_RANGE:
return person.objects.filter(age_range=AGE_RANGE)
else:
return person.objects.all()
def __str__(self):
return self.name
My views
from django.shortcuts import render
from .models import catagory,person,Age
# Create your views here.
def index(request):
c = catagory.objects.all()
categoryID = request.GET.get('category')
AGE_RANGE = request.GET.get('age_range')
p= person.objects.all()
a = Age.objects.all()
if categoryID:
persons = person.get_person_by_cataegoryID(categoryID)
elif AGE_RANGE:
persons = person.get_person_by_age_range(AGE_RANGE)
else:
persons = person.objects.all()
context = {
"person": p,
"catagory": c,
"age": a
}
context["person"] = persons
return render(request, "index.html", context)
My Template
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<h2>hello</h2>
<div style="display:flex;height:100vh;width:100vw;align-items:center;justify-content:space-around;">
<div style="display:flex;flex-direction:column;">
{% for a in age %}
<a href="/?age_range={{a.id}}">{{a.range}}</a>
{% endfor %}
</div>
<div style="display:flex;flex-direction:column;">
<a href="/">All</a>
{% for c in catagory %}
<a href="/?category={{c.id}}">{{c.name}}</a>
{% endfor %}
</div>
<br>
<div style="display:flex;flex-direction:column;">
{% for p in person %}
<p>{{p.name}}</p>
{% endfor %}
</div>
</div>
</body>
</html>
Please tell me what I'm doing wrong here. The filters work by themselves correctly but my goal is to see a specific gender and then that gender's age range. Currently it shows the gender and age range of the whole group.
Solution
if you need gender and age at the same time:
if categoryID and AGE_RANGE:
persons = person.objects.filter(gender=categoryID, age_range=AGE_RANGE)
elif categoryID:
persons = person.get_person_by_cataegoryID(categoryID)
elif AGE_RANGE:
persons = person.get_person_by_age_range(AGE_RANGE)
else:
persons = person.objects.all()
Answered By - Waldemar Podsiadło
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.