python - Flask Flash 未出现在部署中
问题描述
@app.route("/communicate", methods = ["POST"])
def communicate():
data = request.get_json()
for i in data:
flash(i)
print(data)
return "Hello"
我有这个 /communicate 链接,它从事件侦听器获取 json 数据
if (messages.length > 0) {
e.preventDefault()
let data = JSON.stringify(messages)
fetch(`${window.origin}/communicate`, {
method: "POST",
credentials: "include",
body: JSON.stringify(messages),
cache: "no-cache",
headers: new Headers({
"content-type": "application/json"
})
})
location.reload()
}
当我在本地测试完全相同的代码时,for 循环内的闪存正在工作。但是,当我将它部署在 heroku 和 pythonanywhere 上时,它停止了工作。
注意:部署时的 /communicate 路由仍然可以很好地从 js 中获取数据,只是闪烁的每个数据内容都是错误的。
我尝试了这些方法:
- 删除 location.reload()
- 将 get_flashed_messages() 更改为特定的 html 文件,而不是位于 base.html 上
旁注:我使用 location.reload() 刷新页面并加载/更新闪烁的消息。
谢谢~
解决方案
我发现使用诸如 flash() 之类的东西只会给我带来比使用普通 javascript 和使用空的小标签更大的伤害
const name = document.getElementById('username')
const password = document.getElementById('password')
const form = document.getElementById('mainform')
const second_password = document.getElementById("secure_pass")
const password_error = document.getElementById("password-error")
const second_error = document.getElementById("second-error")
const name_error = document.getElementById("name-error")
form.addEventListener('submit', (e) => {
let messages = []
if (name.value === '' || name.value == null) {
messages.push('Filler')
name_error.innerText = "Name is required."
}
else{
name_error.innerText = ""
}
if (password.value != second_password.value || second_password.value != password.value){
messages.push("Filler")
second_error.innerText = "Password doesn't match"
}
else{
second_error.innerText = ""
}
if (password.value.length >= 20) {
messages.push('Filler')
password_error.innerText = "Password should be less than 20 characters"
}
else if (password.value.length <= 8){
messages.push('Filler')
password_error.innerText = "Password should be atleast 8 characters"
}
else{
password_error.innerText = ""
}
if (password.value === 'password') {
messages.push('Password cannot be password \n')
}
if (messages.length > 0) {
e.preventDefault()
}
})
Ps 不介意我只是添加一个计数器以防止提交的“填充物”。
let count = 0
count += 1
更简单
推荐阅读
- elasticsearch - 如何将(文本)字段添加到 pgsync / elasticsearch 中的所有字段?
- python - 使用 defaultdict 根据单独的列值对列值进行配对时,无法转换回日期
- windows - 如何在 Windows cmd 中将命令通过管道传输到 Inkscape --shell?
- laravel - 类圣所\\个人访问令牌未找到
- sql - 需要帮助写一个没有下订单的人的查询
- python-3.x - 如何捕获动态 URL 部分以在 Django 中返回列表
- javascript - 获取 innerText 并拆分为
- email - Twilio Sendgrid - 使用 API 提取个人收件人统计信息?
- javascript - React 测试库:waitFor vs act
- php - 创建一个包含 json 对象数组的 json 对象