Tornado 網站demo 三

來源:互聯網
上載者:User

標籤:methods   use   bsp   資訊   ado   line   跳轉   users   script   

模板

修改index.py

#!/usr/bin/env Python# coding=utf-8import tornado.webimport methods.readdb as mrdclass IndexHandler(tornado.web.RequestHandler):    def get(self):        usernames = mrd.select_columns(table="users",column="username")        one_user = usernames[0][0]        self.render("index.html", user=one_user)    def post(self):        username = self.get_argument("username")        password = self.get_argument("password")        user_infos = mrd.select_table(table="users",column="*",condition="username",value=username)        if user_infos:            db_pwd = user_infos[0][2]            if db_pwd == password:                self.write("welcome you: " + username)            else:                self.write("your password was not right.")        else:            self.write("There is no thi user.")

readdb.py 添加select_columns 方法

def select_columns(table, column ):    sql = "select " + column + " from " + table    cur.execute(sql)    lines = cur.fetchall()    return lines

修改index.html檔案

<!DOCTYPE html><head>    <meta charset="UTF-8">    <meta name="viewport" content="width=device-width, initial-scale=1" />    <title>Learning Python</title></head><body>    <h2> 登入頁面</h2>    <p>用使用者名稱為:{{user}}登入</p>    <form method="POST">        <p><span>UserName:</span><input type="text" id="username"/></p>        <p><span>Password:</span><input type="password" id="password" /></p>        <p><input type="BUTTON" value="登入" id="login" /></p>    </form><script src="{{static_url(‘js/jquery-3.2.1.min.js‘)}}"></script><script src="{{static_url(‘js/script.js‘)}}"></script></body>

要求使用者正確登入之後,跳轉到另外一個頁面,並且在那個頁面中顯示出使用者的完整資訊。

 先修改 url.py 檔案,在其中增加一些內容

#!/usr/bin/env Python# coding=utf-8"""the url structure of website"""#!/usr/bin/env Python# coding=utf-8"""the url structure of website"""from handlers.index import IndexHandlerfrom handlers.user import UserHandlerurl = [    (r‘/‘, IndexHandler),    (r‘/user‘, UserHandler),]

然後就建立 handlers/user.py 檔案

#!/usr/bin/env Python# coding=utf-8import tornado.webimport methods.readdb as mrdclass UserHandler(tornado.web.RequestHandler):    def get(self):        username = self.get_argument("user")        user_infos = mrd.select_table(table="users",column="*",condition="username",value=username)        self.render("user.html", users = user_infos)

 

Tornado 網站demo 三

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.