0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

python3: bottle の使い方 (template)

Last updated at Posted at 2020-11-12

template の使い方です。

フォルダー構造

$ tree
.
├── template.py
└── views
    └── sample.html
template.py
#! /usr/bin/python
#
#	template.py
#
#						Nov/12/2020
#
# ------------------------------------------------------------------
from bottle import run, route, template

# ------------------------------------------------------------------
@route("/")
def index():
    username = '夏目漱石'
    return template('sample', username=username)
# ------------------------------------------------------------------
@route('/hello/<name>')
def hello(name="Stranger"):
    return template("Hello, {{ name }}.  How are you?", name=name)
# ------------------------------------------------------------------
if __name__ == "__main__":
    run(host='localhost', port=8080, reloader=True, debug=True)
# ------------------------------------------------------------------
views/sample.html
<!DOCTYPE html>
<html lang="ja">
<head>
<meta http-equiv="CONTENT-TYPE" content="text/html; charset=utf-8" />
<title>テンプレートエンジン</title>
</head>
<body>
<h2>こんにちは: {{ username }}</h2>
Nov/12/2020<br />
</body>
</html>

サーバーの実行

$ ./template.py 
Bottle v0.12.18 server starting up (using WSGIRefServer())...
Listening on http://localhost:8080/
Hit Ctrl-C to quit.

クライアントでアクセス
http://localhost:8080/
template01.png

http://localhost:8080/hello/太郎
template02.png

次のバージョンで確認しました。

$ python --version
Python 3.9.5

$ python -m bottle --version
Bottle 0.12.19
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?