简体   繁体   English

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

[英]Return SQL queries via Python to JavaScript?

Let's say I have a form: 假设我有一个表格:

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

This form will submit to the URL '/login' in my Flask app. 该表格将提交到我的Flask应用程序中的URL'/ login'。

If the password is incorrect, I would like to pass it to JavaScript somehow so it can display an error on the login page, currently it just redirects to a 'wrong password' page. 如果密码不正确,我想以某种方式将其传递给JavaScript,以便它可以在登录页面上显示错误,当前它只是重定向到“错误密码”页面。 The login information is stored in a SQLite database, so that's why I'm passing it to Python. 登录信息存储在SQLite数据库中,因此正将其传递给Python。

Any Ideas? 有任何想法吗?

You can create a div below the box that receives the input for the credential/credentials that where in valid: 您可以在框下方创建一个div ,以接收有效的凭据输入:

In the HTML: 在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>

Then, in the app (utilizing placeholder lookup functions for username and password): 然后,在应用程序中(为用户名和密码使用占位符查找功能):

@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