1. 前言
最近在看Python web 开发的一些内容, 正好看到了flask, 这里做一些记录
2. flask框架简要说明
- flask 是一个微型的web框架, 相比于 Django 集成了各种模块功能, flask 只是使用了简单的核心, 相关功能可以根据需要进行扩展
3. 一些注意事项
- html 模板文件需要放置在templates文件夹下, 图片文件放在static文件夹下
- 当遇到各种莫名其妙的问题的时候, 其实重新启动一下服务就可以了2333
- 我们之前遇到一个 500 内部错误, 找了好久都没找出来是什么原因, 后来重启了一下, 好了。。。。
- 从form 表单中提取数据, 注意是使用 post 方式
4. 实现效果
5. 实现代码
main.py
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Author: zhyh2010
# @Date: 2016-05-29 14:44:56
# @Last Modified by: anchen
# @Last Modified time: 2016-05-29 15:40:49
from flask import Flask
from flask import render_template
from flask import url_for
from flask import request
app = Flask(__name__)
@app.route('/', methods=['GET', 'POST'])
def index():
if request.method == "POST":
a = request.form['left']
b = request.form['right']
c = int(a) + int(b)
return render_template('index.html', RESULT = str(c))
return render_template('index.html')
if __name__ == "__main__":
app.run(port = 8881)
index.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Calculator</title>
</head>
<body>
<div align="center" style="margin-top:80px">
<!-- <img src="../static/yurisa.jpg" alt="yurisa" height = "400px" /> -->
<img src=" {{url_for('static', filename='yurisa.jpg') }} " alt="yurisa" height = "400px" />
</div>
<div align="center" style="margin-top:30px">
<form method="POST" name = "form1">
<input type="text" name = "left" placeholder = "left"/>+
<input type="text" name = "right" placeholder = "right"/>=
<input type="text" name = "result" placeholder = "result" readonly="readonly" value = "{{ RESULT }}"/>
<input type="submit" value = "submit"/>
</form>
</div>
</body>
</html>