繁体   English   中英

通过Python将SQL查询返回到JavaScript?

[英]Return SQL queries via Python to JavaScript?

假设我有一个表格:

<form action="/login" method="post" class="card-title">
   <input class="form-control" name="username" placeholder="Username" type="text"/>
   <input class="form-control" name="password" placeholder="Password" type="password"/>
   <button class="btn" type="submit">Log  In</button>
</form>

该表格将提交到我的Flask应用程序中的URL'/ login'。

如果密码不正确,我想以某种方式将其传递给JavaScript,以便它可以在登录页面上显示错误,当前它只是重定向到“错误密码”页面。 登录信息存储在SQLite数据库中,因此正将其传递给Python。

有任何想法吗?

您可以在框下方创建一个div ,以接收有效的凭据输入:

在HTML中:

<style>
    .invalid_input{ 
     width:100px;
     height:20px;
     border-radius:4px;
     background-color:red;
     color:white;
   }
 </style>
<html>
<form action="/login" method="post" class="card-title">
  <input class="form-control" name="username" placeholder="Username" type="text"/>
  <div class='invalid_input'>{{message_username}}</div>
    <div class='spacer' style='height:10px;'></div>      
   <input class="form-control" name="password" placeholder="Password" type="password"/> 
  <div class='invalid_input'>{{message_password}}</div>
    <div class='spacer' style='height:10px;'></div>
 <button class="btn" type="submit">Log  In</button>
</form>
</html>

然后,在应用程序中(为用户名和密码使用占位符查找功能):

@app.route('/login', methods = ['GET', 'POST'])
def login():
  if flask.request.method == 'POST':
    username = flask.request.form['username']
    password = flask.request.form['password']
    u_flag, p_flag = is_valid_username(username), is_valid_password(password)
    if not u_flag or not p_flag:
      return flask.render_template('login.html', message_username = '' if u_flag else 'Invalid Username', message_password = '' if p_flag else 'Invalid Password'
  return flask.render_template('login.html', message_username = '', message_password = '')

暂无
暂无

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

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