javascript - 在 Django 中提交表单后显示 javascript 样式警报
问题描述
我正在 Django 中创建一个简单的应用程序。我在页面上有一个表单,提交表单后,我想将用户重定向到一个新页面并显示这样的 JavaScript 警报。我该怎么做呢?我的代码在下面。
这是存储表单数据并重定向到新页面的功能,新页面上只有简单的html。
def donate(request):
if request.method == "POST":
title = request.POST['donationtitle']
phonenumber = request.POST['phonenumber']
category = request.POST['category']
quantity = request.POST['quantity']
location = request.POST['location']
description = request.POST['description']
# New part. Update donor's stats.
UserDetail.objects.filter(user=request.user).update(donations=F('donations') + 1)
UserDetail.objects.filter(user=request.user).update(points=F('points') + (quantity * 2))
return render(request, 'dashboard.html', )
return render(request,'donate.html')
我做了很多研究,但我找不到解决问题的合乎逻辑的方法。过去的问题要求我使用 Django 消息,这是我不想使用的。感谢所有提供帮助的人!
解决方案
在您的模板中,dashboard.html
它似乎是,确保您加载 jquery,然后这将显示一个警报;
向您的页面添加一些上下文以告知模板已提交表单;
return render(request, 'dashboard.html', {'form_submitted': True})
然后您可以检查表单是否已提交
{% if form_submitted %}
<script>
$(document).ready(function(){
alert('Donor stats updated!');
});
</script>
{% endif %}
就个人而言,我会用 djagno 的消息来做这件事——更好的用户体验
推荐阅读
- python - 如何将字符串的最后一个字符替换为字母表的第一个字母?
- php - 如果产品已经在 WooCommerce 购物车中,则禁用添加到购物车按钮
- momentjs - 为什么 MMM 时刻月包含“.”?
- node.js - 当源代码更改时,带有种子随机数的玩笑测试是不可预测的,并且测试失败
- kdb - KDB - 对大型数据集的取消分组或删除进行故障排除?
- sql - 将两个源表连接到一个列中
- mongodb - 使用 Java FindIterable 访问 MongoDB 中的子文档值
- python - 在排序后的数组中查找 np 数组的 ID(或索引),其中可能包含重复元素
- arrays - 在评估不带括号的简单数学表达式时保持运算顺序
- java - 谁调用 describeConstable(),何时调用?