0

I want my template to display my group by category as a header, and then all the values for that group by category under it. For example, my table looks like this:

['John','Physics']
['Jim','Physics']
['Sam','Biology']
['Sarah','Biology']

And I want the template to output this:

Physics

John

Jim

Biology

Sam

Sarah

I'm not sure what to put in my veiws.py as I would usually do this in SQL --> first group by category, then return all results in that category.

How would my veiws.py and template looks like to accomplish this? Thanks.

My current veiws.py:

def department(request):
students = Students.objects.all().order_by('department')
return render(request, 'department.html', {
    'students':students,
})

Here is my model.py

class Mentors(models.Model):
    name = models.CharField(max_length=100)
    degree = models.CharField(max_length=100)
    department = models.CharField(max_length=100)

And my template:

{% if mentors %}
<div class="row">
    {% for mentor in mentors %}
            <div class="col-xs-12 col-sm-6 col-md-6 col-lg-4">
                <h3>{{ mentor.department }}</h3>
            </div>
            <div class="col-xs-12 col-sm-6 col-md-6 col-lg-4">
                <div class="thumbnail">
                    <img src="{{ mentor.image.url }}" class="img-thumbnail">
                    <div class="caption">
                        <h4 class="text-center">{{ mentor.name }}, {{ mentor.degree }}</h4>
                        <p class="text-center"><small>{{ mentor.department }}</small></p>
                    </div>
                </div>
            </div>
    {% endfor%}
</div><!-- /.end row -->
{% endif %}

1 Answers1

0

The template (department.html) is the place where you're going to list all the student. You must have some similar to this:

{% if students %}
    {% for student in students %}
        <p>{{ student.NAME_FIELD }}</p>
        {{ student.DEPARTMENT_FIELD}}
    {% endfor %}
{% else %}
    No student are available.
{% endif %}

NAME_FIELD and DEPARTMENT_FIELD are the fields you declared in your model.py

Luis Alves
  • 194
  • 2
  • 10
  • This is listing every DEPARTMENT_FIELD for each NAME_FIELD. I would like to only list the DEPARTMENT_FIELD once and then list the NAME_FIELD under it. –  Sep 08 '16 at 18:49
  • What you can do is to send within your response, the list of departments. Like you did with student, try something like this. list_departments = Students.objects.values('department').distinct('department') --- then for each department in your template, you list the students that belong to one of them. – Luis Alves Sep 08 '16 at 19:11
  • How do I do that in my template? I thought you can not run QuerySets in your template files? –  Sep 08 '16 at 19:36
  • In your view, you list all departments that you have, using this: list_departments = Students.objects.values('department').distinct('department'), like you did for Students. Then, you send it within your request: return render(request, 'department.html', { 'students':students, 'list_departments':list_departments, }). Now, you have two variable, students and list_department in your template, and you can loop through the list_department. For each department, you loop through the students and check which ones belongs to this department. – Luis Alves Sep 08 '16 at 19:45
  • you can delete each student that you find for a certain department. Then, the list will decrease. – Luis Alves Sep 08 '16 at 19:46