[教师登录他们的帐户后,他/她可以更新学生的行为,而在教师更新其学生的学生行为后,将显示弹出消息(加甜消息)
场景:
这是我想要的结果
这是我的参考:https://github.com/Atrox/sweetify-django
def Updatestudentbehavior(request):
...
sweetify.success(request, 'You did it', text='Good job! You successfully showed a SweetAlert message',
persistent='Hell yeah')
**#update data without refreshing page and popup message will appear**
def teacher(request):
if request.method != 'POST':
raise Http404('Only POSTs are allowed')
try:
......
return render(request, 'Homepage/index.html')
except EmployeeUser.DoesNotExist:
messages.warning(request, 'Your Username and password are Incorrect.')
….
return render(request, 'Homepage/TeacherLogin.html')
def corevalues(request):
...
if StudentsBehaviorGrades.objects.filter(Teacher = teacher).exists():
….
return render(request, 'Homepage/updatebehavior.html',
{'studentsbehaviorgrade':studentsbehaviorgrade,'student':student,'teacher': teacher, 'behavior': behavior, 'behaviorperlevel': behaviorperlevel,
"behaviorperlevels": behaviorperlevels, "period": period})
else:
….
return render(request, 'Homepage/behavior.html',
{'teacher': teacher, 'behavior': behavior, 'behaviorperlevel': behaviorperlevel,
"behaviorperlevels": behaviorperlevels, "Students": Students, "period": period})
urls.py
path('teacher/', Homepage.views.teacher, name='teacher'),
path('Updatestudentbehavior/', Homepage.views.Updatestudentbehavior, name='Updatestudentbehavior'),
我的settings.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>
UPDATE
当我尝试这个时
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)
这是我得到的错误
这是从终端来的
错误本身非常清楚。该页面已被重定向太多次。这是因为您在return redirect(request.path_info)
视图中使用了Updatestudentbehavior
。
如果您查看documentation,您会注意到request.path_info
是当前视图本身的URL。这意味着该视图反复将请求重定向到自身,从而导致浏览器中的错误。
而不是使用request.path_info
,请使用从其生成请求的页面的URL。