日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

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

發布時間:2023/12/20 编程问答 34 豆豆
生活随笔 收集整理的這篇文章主要介紹了 如何在一个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请求?...的全部內容,希望文章能夠幫你解決所遇到的問題。

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