Kervvv
Kervvv

Reputation: 845

Create Sorting Options in Django Template

I am new to Django and in need of some help. I have my database entries being displayed on a template (6 entries). I am aware that to sort your database objects, you need to write Model_Name.objects.all().order_by('Choose_Sort_Option') in the views.py. I would like to make this function a link in that same template. So far, the only thing I've been able to come up with is making a new view, new url pattern, and new template just to display that one new sort. I don't want have to make 30 pages just for sorting. I am thinking that I need to assign that line of code above to a variable(see product d and product_a below). Then i would call that variable in the template, but I'm not sure how to replace the existing list of database items. In short: How do I create clickable links to sort my database objects all within the same template. This is already done in the admin. Thank you!

my views.py:

from django.http import Http404, HttpRequest
from django.contrib import messages
from vendor_db.models import Itemo
from django.db.models import Q
from django.shortcuts import HttpResponse, HttpResponseRedirect,
render_to_response, render, redirect, get_object_or_404

def index(request):
    return render(request, 'vendor_db/index.html')

def vendors(request):
    items = Itemo.objects.all()
    var_2 = request.GET.get("q")
    product_d = Itemo.objects.all().order_by('-Product_Name')
    product_a = Itemo.objects.all().order_by('Product_Name')
    if var_2:
        items = items.filter(Vendor_Name__icontains=var_2)

    context = {'items': items,
    'product_d' :product_d,
    'product_a' :product_a,
    }

    return render(request, 'vendor_db/vendors.html', context)

my template (it is already displaying all of my database objects in an arbitrary order):

{% extends "base.html" %}
{% block content %}
{% load static %}

    <h2>All Vendors</h2>
    <h3>Search</h3><div>
    <form method='GET' action=''>
        <input type='text' name="q" placeholder='Search Vendors'/>
        <input type='submit' value='Search'/>
            <form action="{% url 'vendors' %}"> 
                <input type="submit" value="Reset">
            </form>
    </form>
    <br>
{% if items %}
        {% for stuff in items %}    
            <a href="{% url 'vendors_detail' stuff.id %}">
            {{ stuff.Product_Name|capfirst }}<div></div>
            </a>
        {% endfor %}
{% else %}
    <p>No Results Found</p>
{% endif %}

        <h3>Sort by:</h3>
        <a href="{% url 'product_name_asc' %}">Product Name</a>        
{% endblock %}

EDIT While I am sure the 2 answers below would the do the trick, I was unable to comprehend (my own fault). I places my database entries in a table and used a javascript library to change the sort order just by clicking the column header. You can see the question I asked and the code here

Upvotes: 1

Views: 2894

Answers (2)

SuReSh
SuReSh

Reputation: 1511

In your model.py class use

Class XYZ()
   ...................
   ...................

   class Meta:
       ordering = [
                "your_first_field_name",
                "your_second_field_name",
                ........................
            ]

Upvotes: 1

Georgina S
Georgina S

Reputation: 467

You could use a form with a variable as you said, pass the form back to the same view and get the sort option from that and create the sorted query for your product items dynamically or using if statements. Then rerender the page. This means only one view would be needed.

Or you may be interested in an app called django-tables2, it gives you the ability to render a nice table which can then be sorted as you wish by clicking on arrows.

Upvotes: 0

Related Questions