从Java脚本发出请求以使用Ajax查看功能时未呈现模板

时间:2019-06-28 07:12:21

标签: javascript python django ajax

单击提交按钮后,django模板将调用java-script函数。此java-script函数将请求重定向到要呈现模板的django view函数。

正在调用java-script函数,但是django view函数不会渲染模板,即使在调试时发现两个函数都已被调用。

Django模板:

<button type="submit"  id ="b" name="b" onclick="edit(document.myform.checks,5)" formmethod="get" class="btn btn-danger" disabled>&nbsp &#9998; &nbsp</button>

Django视图功能:

def edit(request,i,unique_key):

      add=False
      template ='pms_app/form.html'
      form=Myform(instance=(Mymodel.objects.get(id=unique_key)))

      if request.method=="POST":

          form=Myform(request.POST,instance=( 
              Mymodel.objects.get(id=unique_key)))
          if form.is_valid():
             form.save()
             return redirect ('pms_app:display',i=i)
          else:
             print(form.errors)


     return render(request, template, context={'form':form,'unique_key':unique_key})

JavaScript函数:

function edit(chk, i) {

var len = 0;
var x=0;
for (j = 0; j < chk.length; j++) {
    if (chk[j].checked == true) {
        len++;
        if (len > 1)
        {
            break;
        }
         x = chk[j].value;
    }
}

var edit_url = 'edit/' + i + '/' + x;

if (len > 1)
{
    alert("Please select only one record to edit!")
}

else
{
    alert(edit_url); 
    $.ajaxSetup({
        data: { csrfmiddlewaretoken: '{{ csrf_token }}' },
    });
    $.ajax({
        url: edit_url,
        type: "GET",
        success: function () {

            alert("requested access complete");
        },
        error: function () {
            alert("requested access failed");
        }
    })

}


}  

关于为什么不呈现模板的任何想法/帮助?

控制台消息:

----------------------------------------
[28/Jun/2019 12:24:30] "GET /app/display/edit/5/23?csrfmiddlewaretoken=%7B%7B+csrf_token+%7D%7D HTTP/1.1" 302 0
[28/Jun/2019 12:24:30] "GET /app/Import HTTP/1.1" 200 5053
[28/Jun/2019 12:24:30] "GET /app/display/5?csrfmiddlewaretoken=JJPL3pWT8edtiMjHvh5Q4lawPH7vMuWtjbNfKGrP47809uPfPd7qrlybqX6sOI66&b=&checks=23 HTTP/1.1" 200 7823
[28/Jun/2019 12:24:31] "GET /static/app/scripts/app.js HTTP/1.1" 304 0

0 个答案:

没有答案