Shell Script 문법 정리
Title: Bash Shell Script 문법 정리 Author: DongDongE Tags: Programming Release: 2021.02.08 [Shell
url_for 함수는 URL을 동적으로 작성하는데 유용합니다.
예를 들어 조건에 맞지 않으면 다른 URL로 리다이렉트 시킬 수 있습니다.
자세한건 아래 예시를 보면서 진행하겠습니다.
from flask import Flask, redirect, url_for # 리다이렉트를 시키기 위해 redirect와 url_for를 참조해야 합니다.
app = Flask(__name__)
@app.route("/")
def hello_world():
return "Hello world!!!"
@app.route("/post/<int:post_id>")
def show_post(post_id):
return "Post id: %d" % post_id
@app.route("/admin/<name>") # /admin/[문자열]
def hello_admin(name):
if name == "admins": # 사용자가 전달한 값이 "admins" 문자열이 존재할 경우
return redirect(url_for("hello_world")) # "/"로 리다이렉트 후 hello_world 함수 실행
else: # "admins"외 다른 값이 존재할 경우
return redirect(url_for("show_post", post_id = 12345)) # "/post/12345"로 리다이렉트 후 show_post 함수 실행
url_for 함수의 전달된 인자는 함수와 변수 + 값을 함께 전달됩니다.