用户更新表单后 Django 弹出消息(模态)
Posted
技术标签:
【中文标题】用户更新表单后 Django 弹出消息(模态)【英文标题】:Django pop up message (modal) after the user updating form 【发布时间】:2020-06-21 15:58:20 【问题描述】:如果用户更新他们的数据并成功弹出消息,我该怎么办?对于我的情况,用户更新数据后需要新标签才能看到成功消息,我想弹出消息而不是新标签页来查看成功消息
我的 html 上有这段代码
<form method="post" action="/reactivate/" id="your-form" enctype="multipart/form-data" autocomplete="on" class="btn btn-info modal-ajax-load"
data-ajax-link="url"
data-toggle="modal"
data-modal-title="tilte"
data-target="#general_modal">% csrf_token %
% for me in ako %
Date: <input type="hidden" value="me.enddate" name="date" id="start_date">me.enddate
<input type="hidden" value="me.id" name="id" hidden><p>me.Email</p>
% endfor %
<select id="days">
% for perfume in s %
<option value="perfume.adddate" data-days="perfume.adddate" id="days">perfume.product - perfume.adddate Days</option>
% endfor %
</select>
<br><br><br>
<label>Deadline:</label><br>
<input type="date" name="endate" id="end_date" readonly/>
<input type="submit" value="Sign up" />
</form>
<div id="general_modal" class="modal fade " >
<div class="modal-dialog ">
<div class="modal-content ">
<div class="modal-header bg-info">
<h6 class="modal-title">Info header</h6>
<button type="button" class="close" data-dismiss="modal">×</button>
</div>
<div class="modal-body">
<!-- Empty will be field by Js -->
</div>
</div>
</div>
</div>
这是我的脚本
<script>
$("#your-form").on('submit',function(e)
e.preventDefault();
var ajax_link = this.getAttribute("data-ajax-link");
var target = this.getAttribute("data-target");
var title = this.getAttribute("data-modal-title");
var size = this.getAttribute("data-modal-size");
var bg = this.getAttribute("data-modal-content-bg");
// $(target+" .modal-body").load(ajax_link);
$.ajax(
url:ajax_link,
type:'POST',
// data: $("#your-form-feilds").serialize(),
success: function (response)
$(target+" .modal-body").html(response);
,
/*
error:function (response)
new PNotify(
title: 'oops',
text:' Unable to load',
type: 'error'
);
*/
);
$(target+" .modal-content").addClass(bg);
$(target+" .modal-title").html(title);
$(target+" .modal-dialog").removeClass().addClass("modal-dialog");
$(target+" .modal-dialog").addClass(size);
</script>
这是我的观点.py
def reactivate(request):
id = request.POST.get('id')
date = request.POST.get('endate')
update = User.objects.get(id=id)
update.enddate = date
update.save()
messages.info(request, 'Your data has been changed successfully!')
return HttpResponse(json.dumps("success":True), content_type="application/json")
【问题讨论】:
【参考方案1】:我有同样的问题,我用奇怪的方式解决了它
id = request.POST.get('id')
date = request.POST.get('endate')
update = User.objects.get(id=id)
update.enddate = date
update.save()
print("dasdasd")
messages.info(request, 'Your Account has been changed successfully!')
obj = your_model.objects.filter(id=id)
s = obj.objects.all()
return render(request, 'myProfile.html', "ako": ako, "s": s)
我没有使用任何脚本或 jquery
% if messages %
% for msg in messages %
<div class="alert alert-info alert-dismissable">
<button type="button" class="close" data-dismiss="alert" aria-hidden="true">×</button>
msg.message
</div>
% endfor %
% endif %
【讨论】:
再次感谢您,感谢您分享您的答案,我会将您的答案标记为正确,因为您是唯一一个不要犹豫帮助我的人。以上是关于用户更新表单后 Django 弹出消息(模态)的主要内容,如果未能解决你的问题,请参考以下文章
python测试开发django-182.jQuery重置form表单