-1

I have a list of results from a user's search and I want the user to be able to re-order them as they wish. It is simple enough to pre-sort the queryset on the backend, in views.py which is what every Google search brings up on the topic. But I need to have this done by the user. On the frontend. This is usually done with a dropdown with options allowing alphabetical sort A-Z or sort by date added or so on.

I can't find an answer with Google search or a single tutorial that covers it, yet I see it used almost everywhere. Does the solution involve ajax? How would I use ajax to do it? Is there a py module that does this in Django?

I am rendering the search results something like this

{% for stor in stories %}
  <div>
    <span class="story_block stock_bg">
       <a href="{{stor.get_absolute_url}}">
       <div class="story_con_block">
       <p class="s_t">{{stor.title}}</p>
       <p>by <strong>{{stor.author.username}}</strong></p>
       <p>{{stor.summary}}</p>
     </div>
    </a>
  </span>
</div>
{% endfor %}
E_net4
  • 27,810
  • 13
  • 101
  • 139
pan
  • 58
  • 2
  • 8

1 Answers1

0

It can be done with javascript if you are rendering the list with javascript. as javascript can manipulate the DOM , so you can sort it with javascript code and add click event to these sorting functions

Ezplora
  • 96
  • 1
  • 4
  • The information in the results that the sort would need is in the individual model objects. How would I access this with javascript? How would the javascript click event access the django queryset to sort it? Can you post an example or point me to some example that I can study? – pan Aug 02 '19 at 15:34
  • 1
    https://stackoverflow.com/questions/739942/how-to-pass-an-array-in-django-to-a-template-and-use-it-with-javascript , check this out – Ezplora Aug 02 '19 at 15:37