【问题标题】:render a view to the client with json request使用 json 请求向客户端呈现视图
【发布时间】:2014-04-22 01:44:32
【问题描述】:

我希望我带着新问题和更清晰的描述回来,因为我做了很多改变,但我还没有找到一个可行的解决方案。从头开始,我有一个视图,每当我按下按钮时,我都想从另一个视图返回渲染视图。

@csrf_exempt def main(请求):

menu_beer = Food.objects.filter(category=4)
menu_crepe = Food.objects.filter(category=2)
menu_club = Food.objects.filter(category=1)
menu_spaggetti = Food.objects.filter(category=8)
menu_burgers = Food.objects.filter(category=11)
menu_hotdog = Food.objects.filter(category=1)
menu_salads = Food.objects.filter(category=7)
menu_toast = Food.objects.filter(category=3)
menu_dessert = Food.objects.filter(category=6)
menu_coffee = Food.objects.filter(category=9)
menu_soda = Food.objects.filter(category=5)
menu_food = Food.objects.filter(category=1)

menu_offer = Offer.objects.all()
obj={}
print "request ajax------------------------"
if request.is_ajax():
    print "inside ajax\/\/\//\/\/\/"
    sItem=request.GET.get('itemId')
    print "GET itemId="+sItem
    if sItem is not None:
        getobject=Food.objects.get(id=int(sItem))
        print getobject
        obj['id']=getobject.id
        obj['title']=getobject.title
        print "{}= "+str(obj)
        return HttpResponse(json.dumps(obj), content_type="application/json")
    else:
        print "ERRRRRRR"

return render(request,'main.html',{'view_title':"Menu",
                                   'menu_crepe':menu_crepe,
                                   'menu_club':menu_club,
                                   'menu_spaghetti':menu_spaggetti,
                                   'menu_burgers':menu_burgers,
                                   'menu_hotdog':menu_hotdog,
                                   'menu_salads':menu_salads,
                                   'menu_toast':menu_toast,
                                   'menu_dessert':menu_dessert,
                                   'menu_coffee':menu_coffee,
                                   'menu_soda':menu_soda,
                                   'menu_beer':menu_beer,
                                   'menu_offer':menu_offer,

                               })

def 配置文件(请求): 返回渲染(请求,'profile.html')

@csrf_exempt def 订单(请求): 对象={} print "请求ajax------------" 如果请求.GET: 打印“POST” sItem=request.GET.get('itemId') 打印 "GET2 itemId="+sItem 如果 sItem 不是无: getobject=Food.objects.get(id=int(sItem)) 打印获取对象 obj['id']=getobject.id obj['title']=getobject.title 打印“post2=”+str(obj) 返回渲染(请求,'order.html',{“obj”:obj}) 别的: 打印“ER” return render_to_response("order.html",{'obj':obj})

js 是这样的:

$(document).ready( function(){
    $(".orderbtn").click(function(){
        p=$(this).prop("id");
        $.ajax({
            type:"GET",
            url:"order/",
            data:{"itemId":p
                  //'csrfmiddlewaretoken': $("{% csrf_token %}")
                 },
            success: function(data){
                $('#selected').html("data.title");
            }

        });

    });
});

所有代码和文件都是https://github.com/b10n1k/foodspot69.git

以及 main.html 中应显示 id="menu_display" 的 div 之间的数据的部分。

<div id="selected" class="selected"></div >

所以,我不确定在这种情况下必须如何处理每个视图。有什么建议我做错了吗?

【问题讨论】:

    标签: ajax json django views


    【解决方案1】:

    我找到了解决方案,并在没有详细信息的情况下发布它以帮助其他人。在成功函数中,我添加了两个参数,现在看起来:

    success: function(data){
                    $('#selected').html(data, textStatus, jqXHR);
                }
    

    我希望有人可以提供更多详细信息,因为我不知道为什么需要它们。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-11-21
      • 1970-01-01
      • 1970-01-01
      • 2017-12-09
      • 2018-07-18
      • 2018-08-10
      • 1970-01-01
      相关资源
      最近更新 更多