Django Ajax无法正常工作

时间:2016-03-29 21:27:26

标签: ajax django

为什么我的数据没有在html板上呈现,函数被调用,我检查过,请看我的js代码和html,控制台没有错误

<!-- HTML -->
<div class="model" type="x-tmpl-mustache"><h1>{{model}}</h1>   </div>


// JavaScript
$(document).ready(function(){
    $.get("/portfolio/model", function(data){
        console.log(data
    $('div.model').html(data);
});


# Python view
class Return_portfolio(LoginRequiredMixin, View): 
    template_name = "dashboard.html"

    def get(self,request):
        user = request.user
        model = Portfolio()
        model_id = model.recommended_portfolio(request, user)
        print(model_id)
        if request.is_ajax():
           return JsonResponse({'model':model_id})

        return render(request, 'dashboard.html',{'portfolio_view':True})

    def post(self,request):
        return HttpResponseNotAllowed(['GET'])

1 个答案:

答案 0 :(得分:0)

试试这个:

<% tags=[{"2":"birds"},{"3":"cars"}] %>

<% tags.each do |t| %>
   <% t.each do |key,value| %>
      <%=key %> <%=value %>
   <% end %>
<% end %>

您的console.log出现语法错误。另外,我更改了jQuery元素id。