TIL - [Django] Render & Redirect

Wooney98ยท2022๋…„ 12์›” 1์ผ
1

TIL

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

https://velog.io/@swson777/Django-%EB%AC%B4%EC%9E%91%EC%A0%95-%EB%94%B0%EB%9D%BC%ED%95%98%EA%B8%B0-4#redirect--render

def loginPage(request):
	if request.method == 'POST':
    	username = request.POST.get('username')
        password = request.POST.get('password')
        user = authenticate(request, username=username, password=password)
        if user is not None:
        	login(request, user)
            return redirect('home')
        else:
        	messages.info(request, 'Username Or Password is Incorrect')
	context = {}
    return render(request, 'accounts/login.html', context)
    

๋งŒ์•ฝ ์—ฌ๊ธฐ์„œ return redirect('home')์ด ์•„๋‹Œ return render(request, 'accounts/login.html')์„ ํ•  ๊ฒฝ์šฐ, ๋˜‘๊ฐ™์ด home.html๋กœ ํ™”๋ฉด์€ ์ด๋™ํ•œ๋‹ค.
ํ•˜์ง€๋งŒ, ์—ฌ๊ธฐ์„œ ์ƒˆ๋กœ๊ณ ์นจ์„ ๋ˆ„๋ฅผ ๊ฒฝ์šฐ, ๋‹ค์‹œ form์„ ์ œ์ถœํ•ด์•ผํ•˜๋Š” ๊ฒฝ์šฐ๊ฐ€ ์ƒ๊ธธ ์ˆ˜ ์žˆ๋‹ค. ๋˜ํ•œ ํŽ˜์ด์ง€ ์ƒ๋‹จ url์„ ๋ณด๋ฉด '.../login/' ๊ตฌ์กฐ์ด๋‹ค. ์•„์˜ˆ ๋กœ๊ทธ์ธ๋œ ํŽ˜์ด์ง€๋ฅผ ์›ํ•˜๊ธฐ ๋•Œ๋ฌธ์— ์ด๋Ÿฐ ์ƒํ™ฉ์—์„œ๋Š” render๊ฐ€ ์•„๋‹Œ redirect๋ฅผ ์“ฐ๋Š”๊ฒƒ์ด ์ ์ ˆํ•˜๋‹ค.

profile
๐Ÿ‘จEducation Computer Engineering ๐ŸŽ“Expected Graduation: February 2023 ๐Ÿ“žContact info thstjddn77@gmail.com

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