最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to return json dictionary in django ajax update - Stack Overflow

programmeradmin5浏览0评论

i am asking this question multiple times since i have not received any applicable help.

my problem is that i dont know how to return query result to template as an ajax response.

i did this:

if request.path == "/sort/":
    sortid = request.POST.get('sortid')
    locs = Location.objects.order_by(sortid)
    if request.is_ajax():
        return HttpResponse(locs,mimetype="application/json")

then my ajax done function does this:

}).done(function(data){
$('.sortierennach').html(data);
});

what now happens is that it just replaces the content of .sortierennach, it is not rendering django dic so that i can do this:

{% for loc in locs %}
  {{loc.name}}
{% endfor %}

can someone please help me... thanks a lot

i am asking this question multiple times since i have not received any applicable help.

my problem is that i dont know how to return query result to template as an ajax response.

i did this:

if request.path == "/sort/":
    sortid = request.POST.get('sortid')
    locs = Location.objects.order_by(sortid)
    if request.is_ajax():
        return HttpResponse(locs,mimetype="application/json")

then my ajax done function does this:

}).done(function(data){
$('.sortierennach').html(data);
});

what now happens is that it just replaces the content of .sortierennach, it is not rendering django dic so that i can do this:

{% for loc in locs %}
  {{loc.name}}
{% endfor %}

can someone please help me... thanks a lot

Share Improve this question edited Feb 27, 2013 at 7:52 Bibhas Debnath 14.9k18 gold badges70 silver badges96 bronze badges asked Feb 27, 2013 at 6:56 doniyordoniyor 37.9k61 gold badges181 silver badges270 bronze badges 1
  • 2 For a sort function of that kind, you should really use a GET method, not POST, since you are not altering any content on the server, so in the future you can attach an ETag or similar to cache the response. – LtWorf Commented Feb 27, 2013 at 7:32
Add a ment  | 

2 Answers 2

Reset to default 4

You'll need to export your object list to a JSON dictionary.

if request.path == "/sort/":
    sortid = request.POST.get('sortid')
    locs = Location.objects.order_by(sortid)
    if request.is_ajax():
        import json
        return HttpResponse(json.dumps(locs), mimetype="application/json")

However, that's gonna require you use some type of client-side template system.

A better way is to use Django's render_to_response shortcut. You don't actually "need" to respond with JSON. You can just respond to the request with a string.

I usually create two templates for AJAX-powered things. The first is a partial template, which contains only the specific bit of HTML that I would want to update during an AJAX-updated. The second is a wrapper, which can be used when the view is called normally.

A cheap example, here's my object_list.html:

<ul id='object-list'>
    {% for object in object_list %}
        <li>{{ object.value }}</li>
    {% endfor %}
</ul>

And here's my base.html:

<html>
<title>Example</title>
    <body>
        {% include 'object_list.html' %}
    </body>
</html>

For the view, you'll want to do this:

from django.shortcuts import render_to_response
from django.template import RequestContext

from models import Location

def view(request):
    locs = Location.objects.order_by(sortid)
    if request.is_ajax():
        return render_to_response('object_list.html', {'object_list': locs}, context_instance=RequestContext(request))
    return render_to_response('base.html', {'object_list': locs}, context_instance=RequestContext(request))

This let's the view get called normally, via a standard GET, or via an XHTTP request, returning only the partial HTML you want to update. Handy!

If you are trying to populate the value in ajax function first you need to convert the queryset object into the json object like

if request.path == "/sort/":
    sortid = request.POST.get('sortid')
    locs = Location.objects.order_by(sortid)
    if request.is_ajax():
        locs = json.dumps(locs)
        return HttpResponse(locs,mimetype="application/json")

Now in your ajax code you will receive the json data .

So by using this locs data either you can generate your html in Ajax or whatever you want to do you can do it .

发布评论

评论列表(0)

  1. 暂无评论