1

Using Django and I would like to change the order of the display of the objects on click without refreshing the page.

my model

class IndexView(generic.ListView):
    template_name = 'movies/index.html'
    page_template = 'movies/all_movies.html'
    context_object_name = 'all_movies'
    model = Movie

    def get_context_data(self, **kwargs):
        context = super(IndexView, self).get_context_data(**kwargs)
        context.update({
            'all_genres': Genre.objects.all(),
            'our_pick': Movie.objects.get(pk=259)
        })
        return context

    def get_queryset(self):
        return Movie.objects.all()

And this is my index.html

<menu class="menu">
   <ul>
     <li><a href="#">Newest</a></li>
     <li><a href="#">Most Popular</a></li>
   </ul>
</menu>

on clink on Newest, the query will become:

Movie.objects.all().order_by('release_date')

and on click on Most popular, the query will become:

Movie.objects.all().order_by('popularity')

How can I achieve that without refreshing the page? any help would be appreciated!

mari
  • 325
  • 1
  • 5
  • 16

1 Answers1

1

Your question seems to illustrate a misunderstanding of how front-end and back-end languages work: a click event occurs on the front-end, after the data has been sent from the server. The order_by function is run and completed before the request is completed.

To load new data from the server without reloading, you will have to send a new request in the background using AJAX.

This is probably not a good idea, though, since you are sending the same query set ordered in a different way. I recommend using JS or jQuery to order the list on a click event based on a data attribute that you can set in the list itself. For more information about how to do this, see jquery sort list based on data attribute value.

Community
  • 1
  • 1
brianpck
  • 8,084
  • 1
  • 22
  • 33