Flask(5)

majaeh43ยท2021๋…„ 12์›” 9์ผ
0

Eager to learn Flask ๐Ÿ”ฅ

๋ชฉ๋ก ๋ณด๊ธฐ
5/7

ํ”Œ๋ผ์Šคํฌ์— ์ง€์›ํ•˜๋Š” ๋ฆฌ๋‹ค์ด๋ ‰ํŠธ ๋ฐ ์—๋Ÿฌ ์ฒ˜๋ฆฌ ํ•จ์ˆ˜

๋ฆฌ๋‹ค์ด๋ ‰์…˜

  • ํŠน์ • URL๋กœ ๊ฐ•์ œ๋กœ ์ด๋™์‹œํ‚ค๊ณ  ์‹ถ์„ ๋•Œ, ์‚ฌ์šฉํ•˜๋Š” ํ•จ์ˆ˜์ธ redirect()
@app.route('/login', methods = ['POST', 'GET'])
def login():
    if request.method == 'POST' and
        request.form['username'] == 'admin':
        return redirect(url_for('success'))
    return redirect(url_for('index'))
    
# form์—์„œ ๋“ค์–ด์˜จ username์ด admin์ด๋ฉด success๋กœ redirect๋˜๊ณ  ์•„๋‹ˆ๋ฉด index๋กœ redirect.

์—๋Ÿฌ

  • ํ”Œ๋ผ์Šคํฌ์—์„œ ์˜ค๋ฅ˜ ์ฝ”๋“œ์™€ ํ•จ๊ป˜ ๊ฐ•์ œ๋กœ ํŽ˜์ด์ง€๋ฅผ ์ค‘์ง€์‹œํ‚ฌ ์ˆ˜ ์žˆ๋Š” ํ•จ์ˆ˜๊ฐ€ abort() ํ•จ์ˆ˜
@app.route('/login', methods = ['POST', 'GET'])
def login():
    if request.method == 'POST':
        if request.form['username'] == 'admin':
            return redirect(url_for('success'))
        else:
            abort(401)
    return redirect(url_for('index'))
# ๋กœ๊ทธ์ธ์— ์‹คํŒจํ•  ๊ฒฝ์šฐ, ์ธ๋ฑ์Šค ํŽ˜์ด์ง€๋กœ ๋ณด๋‚ด๋Š” ๊ฒƒ์ด ์•„๋‹Œ, ์˜ค๋ฅ˜์ฝ”๋“œ 401์™€ ๊ด€๋ จ๋œ ์—๋Ÿฌ ํŽ˜์ด์ง€
AttributeError: 'Flask' object has no attribute 'errorhandelr'. Did you mean: 'errorhandler'?

# ใ…‹ใ…‹ใ…‹ใ…‹ใ…‹ ๋„ˆ๋ฌด ์ฐฉํ•ด... errorhandelr๋ผ๊ณ  ์ ์–ด๋†จ๋„ค? ์•„.... ๊ณ ๋งˆ์›Œใ…Žใ…Žใ…Žใ…Ž ๋‚ด ์—๋Ÿฌ ์žก์•„์ค˜์„œ ^^
profile
๊ฐœ๋ฐœ ๊ธฐ๋ก

0๊ฐœ์˜ ๋Œ“๊ธ€