form 表单需要指定id
<form id="form" action="" method="post" enctype="multipart/form-data">
...
...
<input class="site-login-btn" type="submit" value="提交"/>
...
页面地部需要引入layui js等插件
<script src="{% static 'js/jquery-1.12.4.min.js' %}"></script>
<script src="{% static 'layui/layui.js' %}"></script>
<script src="{% static 'js/jquery.form.js' %}"></script>
<script>
$.ajaxSetup({headers: {"X-CSRFToken": '{{ csrf_token }}'}});
$(function () {
$("#form").ajaxForm(function (data) {
if (data.result == 'SUCCESS') {
layui.use('layer', function () {
var layer = layui.layer;
layer.alert(data.msg, {
closeBtn: 0, btn: ['确认'], yes: function () {
// window.location.href = '此处可以跳转指定url';
window.location.href = document.referrer;
}
});
});
} else if (data.result == 'FAIL') {
layui.use('layer', function () {
var layer = layui.layer;
layer.alert(data.msg, {
closeBtn: 0, btn: ['确认'], no: function () {
window.location.href = document.referrer;
}
});
});
}
});
});
</script>