简体   繁体   English

发送 html 表单到 flask 应用程序后如何接收数据?

[英]How to receive data back after sending a html form to flask app?

I have a HTML site which contains a signup form and login form, the signup and login work as intended in terms of the database - the data is sent to flask and flask updates the database after verifying the data.我有一个 HTML 网站,其中包含一个注册表单和登录表单,注册和登录按数据库的预期工作 - 数据发送到 flask,flask 在验证数据后更新数据库。

In terms of the login page I have used flask to check if the data sent by the form matches the database, it checks the database using SQL and currently I just have flask returning a page which says 'username and password correct' however I'd like to (correct me if there is a better way please) return a bool value eg validLogin as True but I'm not sure how to receive and process this on the HTML/javascript side.在登录页面方面,我使用 flask 检查表单发送的数据是否与数据库匹配,它使用 SQL 检查数据库,目前我只有 flask 返回一个页面,上面写着“用户名和密码正确”但是我会喜欢(请纠正我,如果有更好的方法)返回一个 bool 值,例如 validLogin 为 True,但我不确定如何在 HTML/javascript 端接收和处理它。

I searched around and saw people returned using a dictionary in flask through a built in package known as jsonify but I'm not sure how to 'catch' this data.我四处搜索,看到人们使用 flask 中的字典返回,通过内置的 package 称为 jsonify,但我不确定如何“捕获”此数据。 -> if my data is correct I want to be able to return something like: {"validLogin:": True} then catch this using javascript to allow the user to be logged in. -> 如果我的数据是正确的,我希望能够返回如下内容: {"validLogin:": True}然后使用 javascript 捕获它以允许用户登录。

you can do this by jinja2:您可以通过 jinja2 进行此操作:

# in your python file

from flask import reander_template

#............

@app.route("/...", methods=["GET", "POST"])
def check():
    #check something...
    validLogin=True
    return render_template("example.html", validLogin=validlogin)

and

# in /templates/example.html

<!DOCTYPE html>
<html>
  <body>
    {% if validLogin %}
      <p>login is valid</p>
    {% else %}
      <p>login is not valid</p>
    {% endif %}
  </body>
</html>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM