上传图片注意事项:
1.form表单提交文件,需在form添加属性 enctype="multipart/form-data"
2.当前input标签type = file name="img"
3.views.py img=request.FILE.get("img")
ajax格式:
$("#fav").click(function () {
$.ajax({
# type:"post"
url:"{% url 'project:fav' %}",
data:{
'is_fav':{{ is_fav }},#可穿插模板语言
'pro_id':{{ pro_item.id }},
},
success:function (data) {
window.location.href=data #成功后执行的函数方法 data由后端 HTTPResponse或者JsonResponse传回
}
})
})
onclick 方式(无jq库)
<script type="text/javascript">
function del() {
{#event.preventDefault();#}
console.log('aaa');
{#var href = $(this).attr('href');#}
$.ajax({
url: "{{url_for('admin.user_delete')}}",
type: "POST",
dataType: "json",
data: {
'id':{{ id }}
},
success: function (data) {
location.href = data.location;} #重定向url(view 中 return json.dumps({'location':url_for('admin.user_read')}))
});
}
</script>
form表单:
<form action="{{ url_for('admin.vul_type_delete') }}" method="post">
<input type="hidden" name="id" value={{ id }}>
<input type='submit' value='删除'>
</form>
javaScript
<script>
/*这里是提交表单前的非空校验*/
$("form").submit(function () {
var first = $("input[name='firstname']").val();
var last = $("input[name='lastname']").val();
if (first == "" || first == null || first == undefined) {
alert("first");
return false;/*阻止表单提交*/
} else if (last == "" || last == null || last == undefined) {
alert("last");
return false;/*阻止表单提交*/
} else {
alert("提交");
return true;
}
})
</script>