Flask返回页面及Json数据

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 '

只接受post请求!

' @app.route('/user/') def user(name): return'

hello, %s

' % name @app.route('/hello') @app.route('/hello/') def hello(name = None): return render_template('hello.html',name = name) if __name__ =='__main__': app.run(debug=True)

注意:hello.html要存放在/templates/目录下

hello.html代码如下




    
    Hello from Flask


    

Hello

Hello {{name}}

你可能感兴趣的:(Flask,Python,Python,Flask,返回Json数据)