Flshk返回Html页面及Json数据
#!/usr/bin/python
# -*- coding: utf-8 -*-
import json
from flask import Flask
from flask import request
from flask import redirect
from flask import jsonify
from flask import render_template
app = Flask(__name__)
@app.route('/' , methods=['GET', 'POST'])
def index():
if request.method == 'POST':
a = request.get_data()
dict1 = json.loads(a)
return json.dumps(dict1["data"])
else:
return '<h1>只接受post请求!</h1>'
@app.route('/user/<name>')
def user(name):
return'<h1>hello, %s</h1>' % name
@app.route('/hello')
@app.route('/hello/<name>')
def hello(name = None):
return render_template('hello.html',name = name)
if __name__ =='__main__':
app.run(debug=True)
注意:hello.html要存放在/templates/目录下
hello.html代码如下
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Hello from Flask</title>
</head>
<body>
<h1>Hello </h1>
<h1>Hello {{name}}</h1>
</body>
</html>