Create Sorting Options in Django Template
<p>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 <code>Model_Name.objects.all().order_by('Choose_Sort_Option')</code> 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 <code>product d</code> and <code>product_a</code> 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!</p>

<p>my views.py:</p>

<pre><code>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)
</code></pre>

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

<pre><code>{% extends "base.html" %}
{% block content %}
{% load static %}

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

        &lt;h3&gt;Sort by:&lt;/h3&gt;
        &lt;a href="{% url 'product_name_asc' %}"&gt;Product Name&lt;/a&gt;        
{% endblock %}
</code></pre>

<p><strong>EDIT</strong>
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 <a href="http://www.kryogenix.org/code/browser/sorttable/" rel="nofollow noreferrer">javascript library</a> to change the sort order just by clicking the column header. You can see the question I asked and the code <a href="https://stackoverflow.com/questions/36138669/how-to-use-django-cycle-tag/36138839#36138839">here</a></p>