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