weixin_33701294 2017-02-06 00:48 采纳率: 0%
浏览 36

Django Ajax'GET'

newbie here. I am trying to get some data from my database using ajax.

In my views.py,

def getEvents(request):
    eventList = Events.objects.all()
    events=[]
    for event in eventList:
        events.append({"name": event.name, "start": event.start, "end": event.end})
    return HttpResponse(events, content_type="application/json")

Note that Events is the model that I am trying to parse. After I collect the data from this model, I want to return it to my template using the following ajax code:

            $.ajax({
                url: 'getEvents/',
                datatype: 'json',
                type: 'GET',
                sucess: function(data) {
                    alert(data.name);
                }
            });

In my urls.py:

url(r'^getEvents/', views.getEvents, name='getEvents'),

However, I think I am doing something wrong because it doesn't work. I have been stuck on this for hours...Any ideas?

EDIT:

Okay. When I append the getEvents to the url, I do see all the database objects together in a dict but it seems my ajax is not working. How do I parse this data? The data is in the form:

[{"start": "2017-02-06", "end": "2017-02-07", "name": "Capstone Meeting"},
{"start": "2017-02-07T0:00", "end": "2017-02-08", "name": "Capstone"}, 
{"start": "2017-01-31T0:00", "end": "2017-02-01", "name": "dasdsd"}, 
{"start": "2017-01-31", "end": "2017-02-01", "name": "hjb"}]

Here is what I have so far...

            $.ajax({
                url: 'getEvents/',
                datatype: 'json',
                type: 'GET',
                sucess: function(data) {
                    $.each(data, function(index, element) {
                        $('body').append($('<div>', {
                            text: element.name
                        }));
                    });
                }
            });
  • 写回答

2条回答 默认 最新

  • weixin_33694620 2017-02-06 01:09
    关注

    One of your errors is being caused by not using a JsonResponse in your view instead of an HttpResponse. Here’s how to fix that issue:

    from django.http import JsonResponse
    
    def getEvents(request):
        eventList = Events.objects.all()
        events=[]
        for event in eventList:
            events.append({"name": event.name, "start": event.start, "end": event.end})
        return JsonResponse(events)
    

    From the docs, the JsonResponse is

    An HttpResponse subclass that helps to create a JSON-encoded response.

    The reason that your regular HttpResponse didn’t work, is because you have to manually serialize the data to JSON when using an HttpResponse, e.g., something like:

    import json
    
    response_data = json.dumps(events)
    return HttpResponse(response_data, content_type="application/json")
    

    Otherwise, I think what will happen is that you will get a call to __repr__ on the events list which will get you python ast serialized data and not JSON serialized data.

    评论

报告相同问题?

悬赏问题

  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 LiBeAs的带隙等于0.997eV,计算阴离子的N和P
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘
  • ¥15 来真人,不要ai!matlab有关常微分方程的问题求解决,
  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误
  • ¥199 rust编程架构设计的方案 有偿
  • ¥15 回答4f系统的像差计算