《愚かな方法学Python》ノート37---あなたの最初のウェブサイト
1880 ワード
あなたの最初のサイト
まずフレームを取り付けます
$ sudo pip install lpthw.web
フレームワークとは、通常、あることをより容易にするパッケージです.
プロジェクトスケルトン
.
├── bin
│ ── app.py
│
├── docs
├── gothonweb
│ └── __init__.py
├── templates
│ └── index.html
└── tests
└── __init__.py
5 directories, 4 files
Webアプリケーション
import web
urls = ('/','Index')
app = web.application(urls,globals())
class Index(object):
def GET(self):
greeting = "Hello World"
return greeting
if __name__ == '__main__':
app.run()
プログラムを実行すると、リンクが開き、ページに
Hello World
が表示されます.かたわく
templates/index.html
ファイルの作成$def with (greeting)
Gothons Of Planet Percal #25
$if greeting:
I just wanted to say $greeting.
$else:
Hello, world!
プログラムでテンプレートを呼び出す
import web
urls = ('/','Index')
app = web.application(urls,globals())
render = web.template.render('templates/')
class Index(object):
def GET(self):
greeting = "Hello World"
return render.index(greeting = greeting)
if __name__ == '__main__':
app.run()
ただし、実行後、プロンプト
AttributeError: No template named index
Googleでテンプレートパスの問題を発見
上のスケルトンディレクトリを見てください.pyはbinディレクトリの下にあり、indexテンプレートはbinと平行なtemplatesディレクトリの下にあります.
この問題を解決するには2つの方法があります
render = web.template.render('templates/')
を絶対パスrender = web.template.render('/home/damao/Documents/gothonweb/templates/')
完了すると、テンプレートでレンダリングされたWebページが正常に開きます.
I just wanted to say Hello World.