Weitere ähnliche Inhalte
Ähnlich wie Flask勉強会その1 (20)
Flask勉強会その1
- 10. 解説
flaskフレームワークを使う準備
from
flask
import
Flask
app
=
Flask(__name__)
@app.route("/")
def
hello():
return
"Hello
World!"
ルートにアクセスする時に実行
される処理、ここではクライア
if
__name__
==
"__main__":
app.run()
ントに”Hello World!”を返すだけ
もっと複雑なHTMLを返すにはどうすればいいか?
次章で!
13年10月14日月曜日
- 13. テンプレートエンジン
from
flask
import
Flask
from
flask
import
render_template
app
app
=
Flask(__name__)
hello.py
@app.route('/hello)
@app.route('/hello/<name>')
def
hello_name(name=None):
return
render_template('hello.html',
name=name)
@app.route("/")
def
hello():
return
"Hello
World!"
if
__name__
==
"__main__":
app.run()
templates
hello.html
static
13年10月14日月曜日
<!doctype
html>
<title>Hello
from
Flask</title>
{%
if
name
%}
<h1>Hello
{{
name
}}!</h1>
{%
else
%}
<h1>Hello
World!</h1>
{%
endif
%}
- 16. POSTメソッド
http:/
/hogehoge/
POST /cgi-bin/test.cgi HTTP/1.1
Accept: */*
Referer: http://127.0.0.1/cgi-bin/test.cgi
Accept-Language: ja,en;q=0.5
Content-Type: application/x-www-form-urlencoded
Accept-Encoding: gzip, deflate
User-Agent: Mozilla/4.0 (compatible; MSIE 5.0; Windows 95)
Host: 127.0.0.1
Content-Length: 14
Connection: Keep-Alive
a=alpha&b=beta
13年10月14日月曜日
- 17. GETメソッド演習
from
flask
import
Flask
app
from
flask
import
render_template,request
app
=
Flask(__name__)
get.py
@app.route('/test_get',methods=['GET'])
def
hello_request():
if
request.method
==
'GET':
name
=
request.args.get('name')
return
render_template('hello.html',
name=name)
if
__name__
==
"__main__":
app.run()
templates
hello.html
static
13年10月14日月曜日
<!doctype
html>
<title>Hello
from
Flask</title>
{%
if
name
%}
<h1>Hello
{{
name
}}!</h1>
{%
else
%}
<h1>Hello
World!</h1>
{%
endif
%}
- 18. POSTメソッド演習
from
flask
import
Flask
from
flask
import
render_template,request
app
app
=
Flask(__name__)
get.py
@app.route('/test_get,['PSOT',
'GET'])
def
hello_request():
if
request.method
==
'GET':
name
=
request.args.get('name',
'')
return
render_template('hello.html',
name=name)
if
request.method
==
'POST':
name
=
request.form['name']
return
render_template('hello.html',
name=name)
if
__name__
==
"__main__":
app.run()
templates
hello.html
static
13年10月14日月曜日
<!doctype
html>
<title>Hello
from
Flask</title>
{%
if
name
%}
<h1>Hello
{{
name
}}!</h1>
{%
else
%}
<h1>Hello
World!</h1>
{%
endif
%}
<form
method="POST" >
<p><input type="text" name="name"></p>
<p><input type="submit" value="送信する"></p>
</form>