繁体   English   中英

尝试将用户定向到新的HTML页面Python瓶的问题

[英]Issue Trying To Direct User to New HTML Page Python Bottle

我正在编写一个小型框架,该框架将允许用户登录到网站,但只能在单击主页上的按钮后才能登录。 我有两个HTML文件, site_main_page.htmllogin.html ,以及我的主要Python文件。

site_main_page.html:

<html>
<body>
  <button onclick="toLogin()">Login</button>
  <script>
  function toLogin()
  {
    window.location = "login.html";

  }
   </script>
 </body>
</html>

的login.html:

 <html>
  <body>
    <form action="/login" method="post">
        Username: <input name="username" type="text" />
        Password: <input name="password" type="long" />
        <input value="Login" type="submit" />
    </form>
 </body>
</html>

Python档案:

from bottle import route, run, template, static_file, request, redirect
import urllib
import re
import sqlite3

@route('/')
def main_page():
    return open('site_main_page.html').read()

@route('/login', method='POST')
def user_login():
   username = request.forms.get('username')
   password = request.forms.get('password')
   conn = sqlite3.connect("/site_users")
   cursor = conn.cursor()
   the_data = list(cursor.execute('SELECT username, password from users'))
   if username in the_data:
        return redirect("/")
   else:
        return "<p>credentials invalid </p>"

 run(host="127.0.0.1", port=8000, debug = True)

当前,当我运行主python文件并单击“登录”按钮时,而不是定向到login.html ,我得到了以下消息:

 Error: 404 Not Found
 Not found: '/login.html'

有谁知道如何解决这个问题?

我认为您必须创建一条返回您的login.html的路由

function toLogin()
{
   window.location.assign(window.location.origin + "/login");

}

蟒蛇:

@route('/login')
def login_page():
    return open('login.html').read()

@route('/submitLoginForm', method='POST')
def user_login():
   username = request.forms.get('username')
   password = request.forms.get('password')
   conn = sqlite3.connect("/site_users")
   cursor = conn.cursor()
   the_data = list(cursor.execute('SELECT username, password from users'))
   if username in the_data:
        return redirect("/")
   else:
        return "<p>credentials invalid </p>"

的login.html

<html>
  <body>
    <form action="/submitLoginForm" method="post">
        Username: <input name="username" type="text" />
        Password: <input name="password" type="long" />
        <input value="Login" type="submit" />
    </form>
 </body>
</html>

暂无
暂无

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

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