Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>If you want to use ajax to refresh your page, you'll need to do three things:</p> <ul> <li>Add an entry to urls.py for the ajax call (or add a condition to your view function to process the request if it's ajax)</li> <li>Add the javascript block to make the ajax call and update the html/text with the new data</li> <li>Add the code in your views.py to handle the ajax call and respond with json data</li> </ul> <p><strong>urls.py</strong> ...</p> <pre><code>url(r'/ajax-view-single/)/$', 'ajax_single_item', name='app_name_ajax_single_item'), </code></pre> <p><strong>html/js</strong></p> <pre><code>&lt;script type="text/javascript" src="/js/json2.js"&gt;&lt;/script&gt; $("#view-single-item").click(function () { try { // get slug from html var slug = ""; var data = { slug: slug }; $.get('{% url app_name_ajax_single_item %}', data, function(data){ // your data returned from django is in data alert(data.item_name); }, 'json'); //$('#error').hide(); } catch(err) { $('#error').html(err); $('#error').show(); } return false; }); </code></pre> <p><strong>views.py</strong></p> <pre><code>from django.http import HttpResponse from django.utils import simplejson from django.shortcuts import get_object_or_404 def ajax_single_item(request): '''gets single item''' if not request.is_ajax(): return HttpResponse(simplejson.dumps({'result': False})) # get slug from data slug = request.GET.get('slug', None) # get item from slug item = get_object_or_404(Item, slug=slug) return HttpResponse(simplejson.dumps({ 'result': True, 'item_name': item.name, 'item_price': item.price, 'item_desc': item.desc, 'item_slug': item.slug })) </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload