Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7724679
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T04:44:58+00:00 2026-06-01T04:44:58+00:00

I have these code in my views.py: array = [] p = Person.objects.filter(client=1,status=’Complete’).values(‘name’, ‘age’,

  • 0

I have these code in my views.py:

array = []
p = Person.objects.filter(client=1,status='Complete').values('name', 'age', 'category').order_by('category')
for x in p:
 d = {'Name': x['name'], 'Age': x['age'], 'Category':x['category']}
 array.append(d)

then i got these :

[{'Name': u'Mike', 'Age': 20, 'Category':'A'},
{'Name': u'Bell', 'Age': 30, 'Category':'A'},
{'Name': u'Ned', 'Age': 23, 'Category':'B'},
{'Name': u'Ben', 'Age': 21, 'Category':'B'},
{'Name': u'Tom', 'Age': 28, 'Category':'B'},]

in my template:

{% if persons %}
 {% for p in persons %}
   Category {{p.Category}} :<br>{{p.Name}} <br> {{p.Age}}
 {% endfor %}
{% endif %}

I really want to be out-put is like this:

Category A:
Mike 20
Bell 30

Category B:
Ned 23
Ben 21
Tom 28

do anyone have an idea about my case?
thanks in advance …

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T04:45:00+00:00Added an answer on June 1, 2026 at 4:45 am

    You can use the regroup template tag

    {% if persons %}
        {% regroup persons by "Category" as people_list %}
        {% for key, val in people_list.items %}
            {{ key|title }} : <br /> {{ val|title }}
        {% endfor %}
    {% endif %}
    

    Note that values :

    Returns a ValuesQuerySet — a QuerySet subclass that returns dictionaries when used as an iterable, rather than model-instance objects.

    so you can remove this:

    for x in p:
        d = {'Name': x['name'], 'Age': x['age'], 'Category':x['category']}
        array.append(d)
    

    and change the regroup to:

    {% regroup persons by category as people_list %}
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have several distinct View objects, stored in the views array, that I want
I have several objects in an array. These objects are from a UIImageView subclass.
hi im new to jquery and im facing these problem i have these code
I have a folder of PHP files. The source code to these files is
I have already have this code to force these URLs to HTTPS: RewriteCond %{HTTPS}
I have a bunch of these little bits of HTML code repeated over and
I have this code which will include template.php file from inside each of these
We have two code bases for testing and production. The only differences between these
I have a code base, in which for Matrix class, these two definitions are
I have an array (called array in the code below) which contains a number

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.