在模板中应用Django查询

时间:2019-05-05 18:52:36

标签: django

我可以在HTML页面中编写Django查询,然后将其传递给视图以用作Django原始查询吗? 或告诉我如何根据保管箱所选值更新表数据

$(document).on('Change','#sub_select',function(e){
      e.preventDefault();

      $.ajax({
        type:"POST",
        url:"{% url 'dashboard_app:group_page' %}",
        data:{
          sub_team_val:$("#sub_select").val(),
          csrfmiddlewaretoken:$('Input[name=csrfmiddlewaretoken]').val()
        },
        success:function(){
          console.log("Values Updates!")
        }
      })
    })  

我已经尝试过了,但是没有用,请给我一些好的教程或文档

1 个答案:

答案 0 :(得分:0)

这是我的观点@Ivan Starostin

def Group_View(request):
    if request.user.is_authenticated:
        if request.session.has_key('username'):
            name=request.session['username']
            data_dict= dict()
            if request.method== "POST" and request.is_ajax():
                value=request.POST['sub_team_val']
                data_dict['data']=list(team.objects.all().values())
                data_dict['students']=list(Student.objects.all().values())
                return HttpResponse(json.dumps(data_dict))
            val=request.GET.get('subject_select_value')
            sub_data=Subject.objects.all()
            data=team.objects.all()
            initial_value=Subject.objects.get(pk="CSD101")
            return render(request, "dashboard_app/group.html",{'name':name, 'data':data ,'sub_data':sub_data, 'initial_value':initial_value})
        else:
            return render(request, "Login_app/index.html")
    else:
        return render(request,'Login_app/index.html')