简体   繁体   中英

Get elements of a query-set with a jquery get function. Django

In my django app I would like to display a text when a user clicks on a button, without refreshing the page. When the user clicks again, I would like an other text to be displayed, still without refreshing the page. The informations (texts) I wan't to display are in a query set, named "information".

So I would like to know how to accomplish that.

Here is the way I try to do it:

I create a view where I store my query set:

    def get_information (request):
         information= Information.objects.filter(object1__id= X)

Then I create a jquery get function (in the template of the page where the user clicks) in order to get informations on this list:

    function get_info(){
    $.get('/mysite/get_information', {'information':information}, function(data) {
      $('.information').html(data); 
      });
      };

And then I render it in my template with a submit button and a onclick="get_info".

But I don't know how to make the request get a different information at each request, in order that the user does not get the same information twice.

Thank you a lot for your help.

Take a look in to Django Pagination . You could create the Paginator object in your view and then you can display message from the queryset based on the request from the client side. Let me know in case of any issues.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM