weixin_33695450 2020-01-21 22:45 采纳率: 0%
浏览 202

发送JSON到Django模板

I have a very basic Django view that, once it receives a request, should send a response to the page that the user is visiting, so that i can display a message on that page, without having to refresh the page.

Here is my view:

def Action(request):
    response = ''
    if request.method == 'POST':
        if request.POST.get('ajaxform', False) == 'submit':
            data = request.POST.get('formdata', False)
            mymodel = Userdata()
            mymodel.save()
            response = 'Submitted!'

    return HttpResponse(json.dumps(response), content_type='application/json')

Basically, when a POST request is received, this view should send a JSON response to the template with the message Submitted.

This view works, when i'm browsing my template, on my Dev tools i can see the JSON response Submitted being received by the page, but i don't understand how can i show it using Jquery.

Can someone point me out on how could i create a Jquery function that is triggered only when a Json response is received by a Django view?

Basically, in this case, when the Django view fires the response Submitted, my Jquery function should receive that message and do some stuff with it.

I tried this:

$.ajax({
  url: "/mytemplate/Action",
  type: 'get',
  dataType: 'json',
  success: function (data) {
    console.log(data)
  }
});

The problem with this function, though, is that nothing is being printed in my console, although i can see the response on my Network tab.

  • 写回答

1条回答 默认 最新

  • weixin_33695450 2020-01-21 22:57
    关注

    My guess would be that, since you are calling the json.dumps in a string, your jQuery is also receiving a string. Maybe try calling json.dumps({'response': 'Submitted!') in your view.

    An easier solution may be to use the ".done" callback, which is triggered after the post request is successful. This way, you won't need a specific response from the python view. Doing this, a simple HttpResponse('true') should work.

    More specifics about the .done callback here: https://api.jquery.com/jquery.get/

    Edit after clarification

    The first problem is in your view. There, you specified

    if request.method == 'POST':
    

    but your ajax request is using the GET method

      type: 'get',
    

    Besides that, you still sending a string response, and not an 'stringfied' dictionary.

    I made an working example similar to your post. IMO, your view should be:

    def Action(request):
        response = ''
        if request.method == 'POST':
            if request.POST.get('ajaxform', False) == 'submit':
                data = request.POST.get('formdata', False)
                mymodel = Userdata()
                mymodel.save()
                response = 'Submitted!'
    
        return JsonResponse({'resp': response})
    

    For making the request and dealing with the response, I made a $.post, which is more concise

    $.post("/mytemplate/Action", 
           params,
           function( data )
           {
               console.log(data )
           }
           )
    
    评论

报告相同问题?

悬赏问题

  • ¥15 fluent的在模拟压强时使用希望得到一些建议
  • ¥15 STM32驱动继电器
  • ¥15 Windows server update services
  • ¥15 关于#c语言#的问题:我现在在做一个墨水屏设计,2.9英寸的小屏怎么换4.2英寸大屏
  • ¥15 模糊pid与pid仿真结果几乎一样
  • ¥15 java的GUI的运用
  • ¥15 Web.config连不上数据库
  • ¥15 我想付费需要AKM公司DSP开发资料及相关开发。
  • ¥15 怎么配置广告联盟瀑布流
  • ¥15 Rstudio 保存代码闪退