[英]Issue Trying To Direct User to New HTML Page Python Bottle
我正在編寫一個小型框架,該框架將允許用戶登錄到網站,但只能在單擊主頁上的按鈕后才能登錄。 我有兩個HTML文件, site_main_page.html
和login.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.