tornado之表單和模板


之前在indexHandler中通過self.write()方法在對應的網頁中寫入具體的字符信息。 如果我們想直接返回一個網頁那么這個時候就需要用到模板了

首先在工程目錄下新建一個template文件夾。然后在Application中引用該文件夾的地址。template_path參數告訴Tornado在哪里尋找模板文件。下面的代碼告訴Python在你Tornado應用文件同目錄下的templates文件夾中尋找模板文件。

app = tornado.web.Application(handlers=[(r"/", indexHandler),(r"/show",showHandler)],template_path=os.path.join(os.path.dirname(__file__),"template"),)

然后在template文件下面新建一個index.html文件。內容如下:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8http://127.0.0.1:8000/">
    <title>tornada測試</title>
</head>
<body>
<p>歡迎來到tornado</p>
</body>
</html>

一旦我們告訴Tornado在哪里找到模板,我們可以使用RequestHandler類的render方法來告訴Tornado讀入模板文件,插入其中的模版代碼,並返回結果給瀏覽器

修改indexHandler的get函數。改成return self.render(‘index.html’)

class indexHandler(tornado.web.RequestHandler):
    def get(self, *args, **kwargs):
        return self.render('index.html')

此時再在瀏覽器中輸入http://127.0.0.1:8000/得到如下的輸出

再來看下如何從網頁中傳遞數據到后端。我們首先需要在html中構造傳遞數據的頁面。網頁改成如下,在這里需要輸入名字,年齡,城市,工作。填完后會自動跳轉到/show的頁面

 

<body>

<h1>填入下面的信息</h1>
<form method="post" action="/show">
<p>你的名字<br><input type="text" name="name"></p>
<p>你的城市<br><input type="text" name="city"></p>
<p>你的年齡<br><input type="text" name="age"></p>
<p>你的工作<br><input type="text" name="job"></p>
<input type="submit">
</form>

</body>

輸入http://127.0.0.1:8000/后的頁面如下,點擊提交查詢后會顯示輸入的信息。傳遞參數的方法是post

對應的增加showHandler的函數,定義post函數使用get_argument獲取網頁傳遞的參數,最后通過render函數跳轉到/show的界面。並傳遞獲取到的參數

class showHandler(tornado.web.RequestHandler):
    def post(self, *args, **kwargs):
        name=self.get_argument('name')
        age=self.get_argument('age')
        city=self.get_argument('city')
        job=self.get_argument('job')
        self.render('show.html',name=name,age=age,city=city,job=job)

下面再來看下show.html界面,傳遞的參數通過{{}}方式來引用

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>信息展示</title>
</head>
<body>
<h1>你的信息</h1>
<p>你的名字是{{name}}</p>
<p>你的名城市在{{city}}</p>
<p>你的年齡{{age}}</p>
<p>你的工作是{{job}}</p>
</body>
</html>

最終得到的界面如下

接下來看下如果通過引用靜態文件來渲染模板。在django章節中我們介紹給在django中引用bootstrap來渲染頁面。在tornado中通過也可以引用。

第一步:首先在項目中新建一個static文件夾,並將下載的bootstrap文件放在這個文件夾下面

第二步:在Application添加靜態文件的引用路徑

tornado.web.Application(handlers=[(r"/", indexHandler),(r"/show",showHandler)],template_path=os.path.join(os.path.dirname(__file__),"template"),                           static_path=os.path.join(os.path.dirname(__file__),"static"),)

第三步:在show的head中引用bootstrap

<head>

    <meta charset="UTF-8">

    <title>信息展示</title>

    <link href="/static/css/bootstrap.min.css" rel="stylesheet">

 

    <link href="/static/css/bootstrap.css" rel="stylesheet">

 

    <script src="/static/js/bootstrap.min.js"></script>

 

    <script src="/static/js/bootstrap.js"></script>

 

    <script src="/static/js/jquery-3.2.1.min.js"></script>

</head>

此時的頁面顯示如下

在tornado工程中也可以看到獲取bootstrap的打印

[I 171208 10:18:56 web:2063] 200 GET /static/js/bootstrap.min.js (127.0.0.1) 2.66ms

[I 171208 10:18:56 web:2063] 200 GET /static/js/bootstrap.js (127.0.0.1) 2.57ms

[W 171208 10:18:56 web:2063] 200 GET /static/js/jquery-3.2.1.min.js (127.0.0.1) 51.24ms

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM