我正在尝试使用Ajax在Django中发布表单。我以前已经这样做过,但是现在我找不到我的代码有什么问题。提交表单时,即使我提供了正确的URL,Ajax POST的URL也仍然错误。我想发布到“ / upload / videoUpload /”,但它一直发布到“ / upload”。下面是代码。
HTML:
<form id="videouploadForm" method="POST" >
{% csrf_token %}
<input type="text" id="vidlc" name="video" value="submit" style="display: none" >
<input type="submit" id="sBm120" style="display: none"/>
</form>
AJAX:
<script>
$('#sBm120').trigger('click');
$(document).ready(function() {
$("#videouploadForm").submit(function(event){
event.preventDefault();
$.ajax({
method:"POST",
url:"/upload/videoUpload/",
data: {
'video': $('#vidlc').val(),
'csrfmiddlewaretoken': $('input[name=csrfmiddlewaretoken]').val()
},
success: function(data){
if (data.status){
alert(data.status);
var result = " <video poster='{% static 'images/single-video.png' %}' style='height:30vh' controls='controls' width='100%' height='30%'><source src='http://gateway.ipfs.io/ipfs/"+data.filehash+"' type='video/mp4; codecs='avc1.42E01E, mp4a.40.2'></video>";
document.getElementById("upv").innerHTML=result;
}
}
});
return false; //<---- move it here
});
});
</script>
URLS.py:
path('upload/videoUpload/', uploadVid, name="uploadVideo"),
答案 0 :(得分:0)
您是否尝试过使用Django url模板标签? 网址:“ / upload / videoUpload /”, url:“'{%url” uploadVideo“%}”,
希望它能起作用!
对不起,如果您看不到,请用我的手机写信:)