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

通过Python将SQL查询返回到JavaScript?,javascript,python,html,sqlite,flask,Javascript,Python,Html,Sqlite,Flask,假设我有一张表格: <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"

假设我有一张表格:

<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>

登录
此表单将提交到my 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 = '')