如何在Django中使用sweetify
在教师登录其帐户后,他/她可以更新studentbehavior,并且在教师更新其学生的studentbehavior后,弹出消息将显示sweetify消息 情景: 这是我想要的结果 这是我的参考: url.py如何在Django中使用sweetify,django,django-models,sweetalert,Django,Django Models,Sweetalert,在教师登录其帐户后,他/她可以更新studentbehavior,并且在教师更新其学生的studentbehavior后,弹出消息将显示sweetify消息 情景: 这是我想要的结果 这是我的参考: url.py path('teacher/', Homepage.views.teacher, name='teacher'), path('Updatestudentbehavior/', Homepage.views.Updatestudentbehavior, name='Updatestud
path('teacher/', Homepage.views.teacher, name='teacher'),
path('Updatestudentbehavior/', Homepage.views.Updatestudentbehavior, name='Updatestudentbehavior'),
我的设置.py
INSTALLED_APPS = [
….
'sweetify',
]
SWEETIFY_SWEETALERT_LIBRARY = 'sweetalert2'
我的html
{% load sweetify %}
{% sweetify %}
….
<form class="myform" action="{% url 'Updatestudentbehavior' %}" method="POST" style="width: 100%" >{% csrf_token %}
….
<input type="submit" value="Update" class="myform">
</form>
<script type="text/javascript">
$(document).on('submit', '.myform', function(e) {
e.preventDefault();
console.log(this);
$.ajax({
type:'POST',
url:'{% url 'Updatestudentbehavior' %}'
data:{
teacher:$('#teacher').val(),
student:$('#student').val(),
marking:$('#marking').val(),
csrfmiddlewaretoken:$('input[name=csrfmiddlewaretoken]').val()
},
success:function(){
alert('Update Success');
}
});
});
</script>
<script src="https://code.jquery.com/jquery-3.1.0.min.js"></script>
</body>
</html>
这就是我得到的错误
这是从终点站来的
错误本身很明显。页面被重定向的次数太多。这是因为您在Updatestudentbehavior视图中使用了return redirectrequest.path_info 如果查看,您会注意到request.path_info是当前视图本身的url。这意味着视图会一次又一次地将请求重定向到自身,从而导致浏览器中出现错误
请使用生成此请求的页面的url,而不是使用request.path\u info。检查控制台,您会发现一个未定义Swal的错误 要使sweetify正常工作,您必须添加sweetalert2 cdn: 将此链接放在基本/布局模板中脚本标记的src中
我想我已经试着回答了你的问题,如果你还有其他问题的话。谢谢@Mary这回答了你的问题吗?我如何将它与sweetify联系起来?因为我的问题是关于如何使用sweetify@AKSIf,如果有人对答案投了反对票,那么如果他们也提供一个理由,那将是很有帮助的。这将给我带来一个很好的学习机会。
{% load sweetify %}
{% sweetify %}
….
<form class="myform" action="{% url 'Updatestudentbehavior' %}" method="POST" style="width: 100%" >{% csrf_token %}
….
<input type="submit" value="Update" class="myform">
</form>
<script type="text/javascript">
$(document).on('submit', '.myform', function(e) {
e.preventDefault();
console.log(this);
$.ajax({
type:'POST',
url:'{% url 'Updatestudentbehavior' %}'
data:{
teacher:$('#teacher').val(),
student:$('#student').val(),
marking:$('#marking').val(),
csrfmiddlewaretoken:$('input[name=csrfmiddlewaretoken]').val()
},
success:function(){
alert('Update Success');
}
});
});
</script>
<script src="https://code.jquery.com/jquery-3.1.0.min.js"></script>
</body>
</html>
def Updatestudentbehavior(request):
….
sweetify.success(request, 'You did it', text='Good job! You successfully showed a SweetAlert message',
persistent='Hell yeah')
return redirect(request.path_info)