Authentication
ํ์ ๊ฐ์
๋น๋ฐ๋ฒํธ ์ ๊ณต ๋ฐ ํ์ธ
UserCreateionForm
์ถ๊ฐ column ์ ์์ ์ฅ logic์์ ์ผ์นํ๋์ง ํ์ธ
๋น๋ฐ๋ฒํธ ์ํธํ ์ ์ฅ
User.objects.create_user(username, email=None, password=None)
user.set_password(password)
๋ก๊ทธ์ธ
์ฌ์ฉ์๊ฐ ๋ก๊ทธ์ธ ํ ์ฌ๋์ด๋ค?
Stateless & Connectless
๋งค ์์ฒญ์ด ๋ ๋ฆฝ ์ฌ๊ฑด
cookie
๊ฐ ์ด๊ฑธ ์ด์ด์ค๋ค!
User Object
from django.contrib.auth.models import User
core of the authentication system
'superusers'
or admin'staff'
users are just user objects with special attributes set, not different classes of user objects
AbstractBaseUser
AbstractUser
User

Primary attributes of default user
username
password
email
first_name
last_name
Creating Users
from django.contrib.auth.models import User
user = User.objects.create_user('chloe', 'email-address@gmail.com', 'password-goes-here')
# At this point, user is a User object that has already been saved to the database.
# You can continue to change its attributes, if you want to change other fields.
user.last_name = 'kim'
user.save()
Changing Password
1. Using command line
$ python manage.py changepassword haha
Changing password for user 'haha'
Password:
Password (again):
2. Using set_password()
set_password()
In [6]: ha = User.objects.get(username='haha')
In [7]: ha
Out[7]: <User: haha>
In [8]: ha.set_password('dkgkgkgk')
In [9]: ha.save()
Authenticating Users
authenticate(request=None, **credentials)
use it to verify a set of credentials
takes credentials as keyword arguments
username and password for the default cases
returns
User
object if credentials are valid for a backend
from django.contrib.auth import authenticate
user = authenticate(username='chloe', password='dkgkgkgk')
if user is not None:
# A backend authenticated the credentials
else:
# No backend authenticated the credentials
์ฅ๋ฐ๊ตฌ๋
์ฌ์ฉ์ ---> ์ฅ๋ฐ๊ตฌ๋ ---> ์ฟ ํก
์ฌ์ฉ์ <--- ์ฟ ํค <--- ์ฟ ํก
์ฅ๋ฐ๊ตฌ๋ ==
cookie
๊ตฌ๋งค๋ด์ญ ==
data
๋ก๊ทธ์ธ == create
๋ก๊ทธ์์ == delete
๋ก๊ทธ์ธ Form
from django.contrib.auth.forms import UserCreationForm, AuthenticationForm
AutehticationForm
์ ModelForm ์ด ์๋๋ผ ๊ทธ๋ฅ Form ์ด๋ค!
๋ก๊ทธ์ธ ํจ์
from django.contrib.auth import get_user_model, login
def signin(request):
if request.method == 'POST':
# ์ฌ์ฉ์๊ฐ ๋ณด๋ธ ๊ฐ -> form
form = AuthenticationForm(request, request.POST)
# ๊ฒ์ฆ
# -> ๊ฒ์ฆ ์๋ฃ ์ ๋ก๊ทธ์ธ
if form.is_valid():
login(request, form.get_user())
return redirect('accounts:index')
else:
form = AuthenticationForm()
context = {
'form':form
}
return render(request, 'accounts/signin.html', context)
else
๋ฌธ ์ฒ๋ฆฌ๋ฅผ ๋งค๋๋ฝ๊ฒ ํ๊ธฐ ์ํด ์ฒซ๋ฒ์งธif
๋ก POST๋ฅผ ๋จผ์ ๊ฑฐ๋ฅธ๋คwhy?
๋ง์ฝ GET์ ๋จผ์ ๊ฑฐ๋ฅด๋ฉด, POST์์
.is_valid()
์ ๊ฑธ๋ฆฌ์ง ์๊ณelse
๋ก ๋จ์ด์ง๋ฉด ๋ค์ renderํ๋ ์ฝ๋ ์จ์ค์ผํด์!์ฆ, *code์ ๊ฒฝ์ ์ฑ์ ์ํด POST ๋ฅผ ๋จผ์ ์ด๋ค!
+
POST
๋ก ๋จผ์ ๋ถ๊ธฐํ๋ ์ด์
POST
๋ก ๋จผ์ ๋ถ๊ธฐํ๋ ์ด์ ์ฝ๋์ ๊ฐ๊ฒฐ์ฑ
REST API ๋์
ํ์ฌ ์ฐ๋ฆฌ๋ GET & POST๋ง ๋์ํ๊ณ ์๋๋ฐ ์ดํ์ RESTful ํ๊ฒ ๋ฉ์๋ ๊ตฌ์ฑํ ๊ฒฝ์ฐ GET/POST/PUT/DELETE ์ฌ๋ฌ๊ฐ์ ๋ฉ์๋๊ฐ ์ค๊ฒ ๋๊ณ GET method๊ฐ ๋ง์ง๋ง์์ ํธ๋ค๋ง๋๋ ํํ๊ฐ ๊ฐ์ฅ ๊ฐ๊ฒฐํ ์ฝ๋ ๊ตฌ์ฑ์ด ๊ฐ๋ฅ!
Message Framework
new
-> ๊ธ ์์ฑ ํ์ด์ง (form)
create
-> DB์์ ์ฅ
-> render
-> redirect(์ฑ๊ณต์ฌ๋ถ)
-> redirect('articles:index')
HTTP๋ request์ response์ ๋ฐ๋ณต์ด๋ค!
HTTP
stateless (๋ฌด ์ํ์ฑ)
ํ๋ฒ ์์ฒญ์ ๋ณด๋ด๋ฉด ์ํ(๊ณผ๊ฑฐ)๋ฅผ ์ ์ ์์
๋ชจ๋ ์์ฒญ & ์๋ต์ ์ผํ์ฑ์ด๋ค
HTTP๋ ๋จ์ ์ ์ธ protocol
connectionless (๋ฌด ์ฐ๊ฒฐ์ฑ)
Message Framework
์ด์ ์ ์ํ๋ฅผ ๋ค์
Request
&Response
์ ๋๊ฒจ์ค๋ค๋ ๊ฒ์ด ์๋ฏธ๊ฐ ์๋คFallback Storage
Cookie ๊ฐ ์๋๋ฉด Session
Dynamic view
Article CRUD
title, content, create_at, updated_at
User CRUD (์ง์ < Django)
+
in memory cache -> ram์ ๋์๋๋ cache๋ผ๊ณ ์๊ฐํ๋ฉด ๋จ
memcached
redis
๊ตฌ๊ธ ๊ด๊ณ ์์ด๋......gdpr
macaddress = ๊ธฐ๊ธฐ์ ๋ณด
Last updated
Was this helpful?