[TIL] Flask - router와 method

jake.log·2021년 11월 23일
0

Flask

목록 보기
2/3

flask 라우팅을 사용하는 방법

from flask import jsonify, redirect, url_for 

@app.route('/test/name/<name>')
def name(name):
    return f"name is {name},{escape(type(name))}"
    
@app.route('/test/id/<int:id>')
def id(id):
    return 'Id: %d' % id

@app.route('/test/path/<path:subpath>')
def path(subpath):
    return subpath

@app.route('/test/json')
def json():
    return jsonify({'hello':'world'}) 

@app.route('/test/redirect/<path:subpath>')
def redirect_url(subpath):
    return redirect(subpath)

@app.route('/test/urlfor/<path:subpath>')
def urlfor(subpath):
    return redirect(url_for('path',subpath=subpath))

flask mehod 사용하는 방법

from flask import request

@app.route('/test/method/<id>', methods=['GET','POST'])
def method(id):
    return jsonify({
        'request.args':request.args,
        'request.form':request.form,
        'request.json':request.json,
        'request.method':request.method
    })
profile
꾸준히!

0개의 댓글