日韩av黄I国产麻豆传媒I国产91av视频在线观看I日韩一区二区三区在线看I美女国产在线I麻豆视频国产在线观看I成人黄色短片

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 >

如何在一个html页面中提交两个post,如何在同一个页面上从Django和Ajax获得多个post请求?...

發布時間:2023/12/20 46 豆豆
生活随笔 收集整理的這篇文章主要介紹了 如何在一个html页面中提交两个post,如何在同一个页面上从Django和Ajax获得多个post请求?... 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

我一整天都在為這事犯愁。似乎什么都沒用。這是我的情況。在

我有一個Django表單,有兩個字段:redirect_from,redirect_to。此表單有兩個提交按鈕:Validate和{}。當頁面加載時,Submit被隱藏,只顯示Validate。在

所以現在,當用戶填充這兩個字段并單擊Validate時,我使用Ajax來確認這兩個字段是相同的。如果是,則顯示Save按鈕。單擊Save按鈕應將表單保存到數據庫中。我還添加了一個oninput監聽器,這樣在Ajax調用之后,如果用戶試圖更改數據,Save按鈕將再次隱藏,他現在必須再次計算它。在

顯然,使用Ajax應該很容易,但是我發現它非常困難和令人沮喪。到目前為止,我的代碼是:

我的模板:form method="post">

{% csrf_token %}

{% include 'partials/form_field.html' with field=form.redirect_from %}

{% include 'partials/form_field.html' with field=form.redirect_to %}

Save

Validate

{% endblock %}

{% block extra_scripts %}

{{ block.super }}

$(document).ready(function() {

$("#submit").hide()

});

$('#id_redirect_to').on('input', function(){

$("#submit").hide()

});

console.log("hello")

//For getting CSRF token

function getCookie(name) {

var cookieValue = null;

if (document.cookie && document.cookie != '') {

var cookies = document.cookie.split(';');

for (var i = 0; i < cookies.length; i++) {

var cookie = jQuery.trim(cookies[i]);

if (cookie.substring(0, name.length + 1) == (name + '=')) {

cookieValue = decodeURIComponent(cookie.substring(name.length + 1));

break;

}

}

}

return cookieValue;

}

//For doing AJAX post

//When submit is click

$("#ajax_submit").click(function(e) {

console.log("Clicked")

e.preventDefault();

//Prepare csrf token

var csrftoken = getCookie('csrftoken');

//Collect data from fields

/*var email = $('#inputEmail').val();*/

var redirect_from= $('#id_redirect_from').val();

var redirect_to= $('#id_redirect_to').val();

console.log("URL from and to is", redirect_from, redirect_to)

/*var password = $('#inputPassword').val();*/

//Send data

$.ajax({

url : window.location.href, // the endpoint,commonly same url

type : "POST", // http method

data : { csrfmiddlewaretoken : csrftoken,

redirect_from : redirect_from,

redirect_to : redirect_to

/*password : password*/

}, // data sent with the post request

// handle a successful response

success : function(json) {

console.log(json); // another sanity check

//On success show the data posted to server as a message

if (json['redirect_success'] === 'true')

{

alert("They are the same!" +json['redirect_success'] +'!.' );

$("#submit").show()

}

else

{

$("#submit").hide() //Maybe display some error message in the future!

}

},

// handle a non-successful response

error : function(xhr,errmsg,err) {

console.log(xhr.status + ": " + xhr.responseText); // provide a bit more info about the error to the console

}

});

});

{% endblock extra_scripts %}

我的觀點:

^{pr2}$

我得到了500: MultiValueDictKeyError at "'Save'"

有人能把我引向正確的方向嗎?對ajax很陌生。在

總結

以上是生活随笔為你收集整理的如何在一个html页面中提交两个post,如何在同一个页面上从Django和Ajax获得多个post请求?...的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。