简体   繁体   中英

Issue Trying To Direct User to New HTML Page Python Bottle

I am writing a small framework that will allow users to login to the site but only after clicking a button on the homepage. I have two HTML files, site_main_page.html and login.html , along with my main Python file.

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

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)

Currently, when I run the the main python file and click on the "Login" button, instead of being directed to login.html , I get the message:

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

Does anyone know how this issue can be resolved?

I think you have to create a route that returns your login.html

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

}

python:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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