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 8473855
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T17:28:05+00:00 2026-06-10T17:28:05+00:00

Let these models: class Category(models.Model): name = models.CharField(max_length=20) class Word(models.Model): name = models.CharField(max_length=200) votes

  • 0

Let these models:

class Category(models.Model):
    name = models.CharField(max_length=20)

class Word(models.Model):
    name = models.CharField(max_length=200)
    votes = models.IntegerField(default=1)
    categories = models.ManyToManyField(Category, null=True, blank=True)

this view:

def main_page(request):
    words = Word.objects.all()
    categories = Category.objects.all()
    return render(request, "main_page.html", {'words': words})

and this template:

{% for category in categories %}
    {% for word in category.word_set.all %}
    <p>{{ word }}</p>
    {% endfor %}
{% endfor %}

I’d like to sort words in template by number of votes and by pub date, separately. How can I do this?

  • 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-10T17:28:07+00:00Added an answer on June 10, 2026 at 5:28 pm

    You can make custom template tag or filter, which gets words set and sorting type as parameters.

    For example (haven’t tested):

    custom_tags.py:

    from django import template
    register = template.Library()
    
    @register.filter
    def sort_by(queryset, order):
        return queryset.order_by(order)
    

    template.html

    {% load custom_tags %}
    ...
    {% for word in category.word_set.all|sort_by:'-votes' %}
        <p>{{ word }}</p>
    {% endfor %}
    ...
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Let these models: class Category(models.Model): name = models.CharField(max_length=20) class Word(models.Model): name = models.CharField(max_length=200) categories
Let's say I have these models: class A(models.Model): name = models.CharField(max_length=32) b = models.ForeignKey(B,
I have a model with an optional file field class MyModel(models.Model): name = models.CharField(max_length=50)
Let's assume we have following models: from django.db import models class Foo(models.Model): name =
Given the following models (cut down for understanding): class Venue(models.Model): name = models.CharField(unique=True) class
Let's say I have these two variables $number = 1; $word = one; and
Let's say I have these class hierarchy : public abstract class Parent { }
Let's say we have these two classes: public class Base { public static int
Let's suppose I have all of these already provided: The name of a table
Let's say I have the following models: Class Wishlist belongs_to :user # User class

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.