2017-09-07 3 views
0

私はホームページにボタンをクリックした後で、ユーザーがサイトにログインできる小さなフレームワークを作成しています。私は、メインのPythonファイルとともに、site_main_page.htmllogin.htmlという2つのHTMLファイルを持っています。問題新しい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' 

誰もがこの問題を解決できる方法を知っていますか?

答えて

1

私はあなたの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> 
+0

はご回答いただきありがとうございます。しかし、スクリプトを再実行しようとすると、元の '404'エラーメッセージが表示されました。他のファイルで変更する必要があるコードはありますか? – Ajax1234

+0

あなたのサーバにlogin.htmlを返すルートがありますか? –

+0

現在、私は '@route( '/ login'、method = 'POST')デコレータしか持っていません。 – Ajax1234

関連する問題