首页 > 解决方案 > 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() 刷新页面并加载/更新闪烁的消息。

谢谢~

标签: pythonflaskherokudeploymentpythonanywhere

解决方案


我发现使用诸如 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 

更简单


推荐阅读