๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ

๐Ÿ’ชBackend/Django

Django๋กœ Pinterest ๋”ฐ๋ผ๋งŒ๋“ค๊ธฐ : ์„น์…˜ 3. Authentication (27~29๊ฐ•)

#27 Authentication ์ธ์ฆ์‹œ์Šคํ…œ ๊ตฌ์ถ•

 

authentication

์šฐ๋ฆฌ๊ฐ€ account๋ฅผ ๋งŒ๋“  ์ด์œ ?

: ์•„๋ฌด๋‚˜ ๋ชจ๋“  ๊ธฐ๋Šฅ์„ ์‹คํ–‰ํ•˜๋Š” ๊ฒƒ์„ ๋ฐฉ์ง€ํ•˜๊ธฐ ์œ„ํ•ด์„œ ๋ˆ„๊ฐ€ ๋ˆ„๊ตฐ์ง€ ํ™•์ธ ํ•„์š”.

 

 

view ์ˆ˜์ •

views์—์„œ request.user.is_authenticated ๋กœ ๋กœ๊ทธ์ธ๋˜์–ด์žˆ๋Š”์ง€ ์•„๋‹Œ์ง€์— ๋”ฐ๋ผ ๋‹ค๋ฅธ ๋™์ž‘ ํ•˜๋„๋ก ์ˆ˜์ •

 

 

๋กœ๊ทธ์ธ ์•ˆ๋˜์–ด์žˆ๋Š” ๊ฒฝ์šฐ, ์œ„์ฒ˜๋Ÿผ ๋กœ๊ทธ์ธ์ฐฝ์œผ๋กœ redirect ๋จ

 

class์•ˆ์— ํ•จ์ˆ˜๋ฅผ ์„ ์–ธํ•ด์„œ get ๋‚ด๋ถ€ ํ–‰๋™์„ ์ง€์ •ํ•ด์ค„ ์ˆ˜ ์žˆ์Œ.

def get(self, *args, **kwargs):
	if self.request.user.is_authenticated:
		return super().get(*args,**kwargs)
	else: 
		return HttpResponseRedirect(reverse('accountapp:login'))

์œ„์ฒ˜๋Ÿผ~~

 

 

์ด๋ ‡๊ฒŒ๋งŒ ํ–ˆ์„๋•Œ์˜ ๋ฌธ์ œ๋Š” test1 ์œผ๋กœ ๋กœ๊ทธ์ธํ•˜๊ณ  ๋‹ค๋ฅธ ์‚ฌ๋žŒ ๊ณ„์ •์˜ ์ •๋ณด ์ˆ˜์ • ํŽ˜์ด์ง€์— ์ ‘๊ทผ ๊ฐ€๋Šฅํ•จ..ํƒˆํ‡ด๊นŒ์ง€๋„ ๊ฐ€๋Šฅ

⇒ pk์— ํ•ด๋‹นํ•˜๋Š” object ์ฆ‰, user๊ฐ์ฒด๋ฅผ ๊ฐ€์ ธ์™€์„œ request๋ฅผ ๋ณด๋‚ด๊ณ  ์žˆ๋Š” ์œ ์ €์™€ ๊ฐ™์€์ง€ ํ™•์ธ!

 

 


 

 

 

#28 Decorator๋ฅผ ์ด์šฉํ•œ ์ฝ”๋“œ ๊ฐ„์†Œํ™”

 

27๊ฐ•์—์„œ ์ž‘์„ฑํ–ˆ๋˜ ์ฝ”๋“œ๋ฅผ ๊ฐ„์†Œํ™”์‹œํ‚ค๊ธฐ

Decorator

 

ํŒŒ์ด์ฌ์—์„œ ์ œ๊ณตํ•˜๋Š” ๊ธฐ๋Šฅ,

ํ•จ์ˆ˜๋ฅผ ์•ž ๋’ค๋กœ ๊พธ๋ฉฐ์ค„ ์ˆ˜๋„ ์žˆ๊ณ  … ํ•จ์ˆ˜๋ฅผ ๊พธ๋ฉฐ์ฃผ๋Š” ํ˜•ํƒœ ํ•จ์ˆ˜์˜ ๋‚ด๋ถ€๋ฅผ ๊ณ ์น˜์ง€๋Š” ์•Š์ง€๋งŒ ์•ž๋’ค์— ๋ถ™์–ด์„œ~.. ์ฝ”๋“œ๊ฐ€ ํ›จ์”ฌ ๊ฐ„์†Œํ™”๋จ !

๊ฒน์น˜๋Š” ๋‚ด์šฉ๋“ค์„ ํ•˜๋‚˜๋กœ ๋นผ๊ณ  ๊ทธ๊ฑธ decorator๋กœ ๊ฐ์‹ธ์„œ ์•ž๋’ค๋กœ ์ˆ˜์ •ํ•ด์ฃผ๊ธฐ

 

 

def hello_world ์ˆ˜์ •

@login_required

์œ„ ์ฒ˜๋Ÿผ ๋ถˆ๋Ÿฌ์˜ค๋ฉด ๋จ!

 

if user.is_authenticated ํ–ˆ๋˜๊ฑฐ ์ž๋™์œผ๋กœ ํ•ด์คŒ

 

 

class AccountUpdateView ์ˆ˜์ •

class์— ์ ์šฉํ• ๋•Œ๋Š” ๋˜ ์กฐ๊ธˆ ๋‹ค๋ฅด๋‹ค.

class ์•ˆ์— ์žˆ๋Š” method์—๋Š”

@method_decorator

์–˜๋ฅผ ๊ฐ€์ ธ์™€์„œ ์ด ์•ˆ์— ๋„ฃ์–ด์คŒ

: ์ผ๋ฐ˜ function์— ์‚ฌ์šฉํ•˜๋Š” ๋ฐ์ฝ”๋ ˆ์ดํ„ฐ๋ฅผ method์—์„œ๋„ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋„๋ก ๋ณ€ํ™˜ํ•ด์ฃผ๋Š” ๋ฐ์ฝ”๋ ˆ์ดํ„ฐ

 

 

@method_decorator(login_required,'get')

@method_decorator(login_required, 'post')

 

์ด๋Ÿฐ์‹์œผ๋กœ~~

 

 

login_required๋Š” ๋กœ๊ทธ์ธํ–ˆ๋Š”์ง€๋งŒ ํ™•์ธํ•ด์ฃผ๋Š” ๋ฐ์ฝ”๋ ˆ์ดํ„ฐ์ด๊ธฐ ๋•Œ๋ฌธ์— ๋ณธ์ธ์ธ์ง€ ํ™•์ธํ•˜๊ธฐ ์œ„ํ•œ ๋ฐ์ฝ”๋ ˆ์ดํ„ฐ๋ฅผ ๋งŒ๋“ค์–ด์ฃผ์–ด์•ผํ•จ!

 

 

 

custom decorator

accountapp ๋‚ด์— decorator ํŒŒ์ด์ฌ ํŒŒ์ผ ๋งŒ๋“ค์–ด์„œ

from django.contrib.auth.models import User
from django.http import HttpResponseForbidden

def account_ownership_required(func):
    def decorated(request, *args, **kwargs):
        user = User.objects.get(pk=kwargs['pk'])
        if not user == request.user:
            return HttpResponseForbidden()
        return func(request, *args, **kwargs)
    return decorated

์œ„์ฒ˜๋Ÿผ ์ž‘์„ฑํ•ด์ฃผ๋ฉด ์ง€๊ธˆ ๋กœ๊ทธ์ธํ•œ ์œ ์ €์™€ ํ•ด๋‹น ์œ ์ €๊ฐ€ ๊ฐ™์€์ง€ ํ™•์ธ ๊ฐ€๋Šฅ ( user ๊ฐ€ request๋ฅผ ์š”์ฒญํ•œ ์œ ์ €์™€ ๊ฐ™์€์ง€ ํ™•์ธ )

 

 

๋‹ค์ˆ˜์˜ ๋ฐ์ฝ”๋ ˆ์ดํ„ฐ

 

์—ฌ๋Ÿฌ๊ฐœ์˜ ๋ฐ์ฝ”๋ ˆ์ดํ„ฐ๊ฐ€ ํ•„์š”ํ•œ ๊ฒฝ์šฐ, ๊ฐ๊ฐ์˜ ๋ฐ์ฝ”๋ ˆ์ดํ„ฐ๋ฅผ ๋ชจ๋‘ ์ ์–ด์ค„ ์ˆ˜๋„ ์žˆ์ง€๋งŒ,

 

has_ownership = [account_ownership_required, login_required]

 

์œ„์ฒ˜๋Ÿผ ๋ฐฐ์—ด๋กœ ๋งŒ๋“ค์–ด์„œ

 

@method_decorator(has_ownership, 'get')

@method_decorator(has_ownership, 'post')

 

์ด๋Ÿฐ์‹์œผ๋กœ ์ ์–ด์ค„ ์ˆ˜ ์žˆ์Œ. ๊ธฐ์–ตํ•˜๊ธฐ

 

 

 


 

 

#29 superuser, media ๊ด€๋ จ ์„ค์ •

 

profile ์•ฑ์„ ๋งŒ๋“ค๊ธฐ ์ „์— ์‚ฌ์ „์„ค์ •ํ•ด์ฃผ๋Š” ์‹œ๊ฐ„

 

superuser

python manage.py createsuperuser

๋กœ superuser ์ƒ์„ฑํ•ด์คŒ

์ด๊ฑธ๋กœ adminํŽ˜์ด์ง€ ๋“ค์–ด๊ฐ€๋ฉด ๋‹ค ๊ฐ€๋Šฅ

 

 

media ์„ค์ •

MEDIA_URL = '/media/'
MEDIA_ROOT=os.path.join(BASE_DIR,'media')

settings.py ์—์„œ ์œ„์™€ ๊ฐ™์ด ๋ฏธ๋””์–ด ๊ฒฝ๋กœ ์„ค์ • media/ ์ดํ•˜์˜ ํŒŒ์ผ๋“ค์— ์ ‘๊ทผ ๊ฐ€๋Šฅ

 

pip install pillow

media ์ด์šฉํ• ๋•Œ ํ•„์š”ํ•˜๋ฏ€๋กœ ์„ค์น˜

 

 


๋ฐ•ํ™์„๋‹˜์˜ ์ธํ”„๋Ÿฐ ๊ฐ•์˜๋ฅผ ์ˆ˜๊ฐ•ํ•˜๋ฉฐ ์ •๋ฆฌํ•ด๋ณด๋Š” ๊ธ€์ž…๋‹ˆ๋‹ค.

https://www.inflearn.com/course/%EC%9E%A5%EA%B3%A0-%ED%95%80%ED%84%B0%EB%A0%88%EC%8A%A4%ED%8A%B8

 

์ž‘์ •ํ•˜๊ณ  ์žฅ๊ณ ! Django๋กœ Pinterest ๋”ฐ๋ผ๋งŒ๋“ค๊ธฐ : ๋ฐ”๋‹ฅ๋ถ€ํ„ฐ ๋ฐฐํฌ๊นŒ์ง€ ๊ฐ•์˜ - ์ธํ”„๋Ÿฐ

Python์˜ ๋Œ€ํ‘œ ์›น ํ”„๋ ˆ์ž„์›Œํฌ django๋ฅผ ์ด์šฉํ•ด Pinterest ํŠน์œ ์˜ ์นด๋“œํ˜• ๋ ˆ์ด์•„์›ƒ ๋””์ž์ธ์„ ๋ณธ๋”ด ์›น์„œ๋น„์Šค ๊ตฌํ˜„ํ•ด๋ด…๋‹ˆ๋‹ค., django, docker ์‹ค์ „ ๊ฐœ๋ฐœ์„ ํ†ตํ•ด ํ•œ ๋‹จ๊ณ„ ๋†’์€ ๊ฐœ๋ฐœ์ž๊ฐ€ ๋˜