[ User ๊ธฐ๋ณธ ]
1. python manage.py startapp account ๋ฅผ ํตํด ๊ณ์ ๊ด๋ จ ์ฑ ์๋ก ์์ฑํ๋ค
2. settings.py์์ INSTALLED_APPS์ ์ฑ ์์ฑ ์๋ ค์ฃผ๊ธฐ
3. templatesํด๋์ login.html๊ณผ signup.html ์์ฑ(๋ก๊ทธ์์์ ํ์์์)
4. views.py์์ ๋ก๊ทธ์ธ ํผ๊ณผ ํ์๊ฐ์ ํผ ์์ฑ
from django.shortcuts import render, redirect
from django.contrib.auth.forms import AuthenticationForm, UserCreationForm
from django.contrib.auth import authenticate, login, logout
5. views.py
def login_view(request):
if request.method == 'POST' #post๋ฐฉ์, get๋ฐฉ์์ ๋ฐ๋ผ ๋ค๋ฅด๊ฒ ๋์๊ฐ๋ ๋ก์ง
form = AuthenticationForm(request=request, data=request.POST)
if form.is_valid(): #์ ํจ์ฑ ๊ฒ์ฌ
username = form.cleaned_data.get("username")
password = form.cleaned_data.get("password")
user = authenticate(request=request, username=username, password=password)
if user is not None: #์ ์ ๊ฐ ์กด์ฌํ ๋
login(request, user)
return redirect("home")
else:
form = AuthenticationForm() #๋ก๊ทธ์ธํผ ์์ฑ
return render(request, 'login.html', {'form':form})
def logout_view(request):
logout(request)
return redirect("home")
def register_view(request):
if request.method == 'POST'
form = UserCreationForm(request.POST)
if form.is_valid():
user = form.save() #์ ํจํ๋ฉด ์ ์ฅ
login(request, user)
return redirect("home")
else:
form = UserCreationForm()
return render(request, 'signup.html', {'form':form})
6. account์ฑ์ urls.py ์์ฑ
from django.urls import path
from .views import *
urlpatterns = [
path('login/', login_view, name="login"),
path('logout/', logout_view, name="logout"),
path('register/', register_view, name="signup"),
]
7. projectํด๋ urls.py์ path('account/', include('account.urls')),
8. login.html, signup.html์ action="{% url 'login / logout / signup' %}"
9. {% if user.is_authenticated %} ๋ก ์ ํจํ ์ ์ ์ธ์ง(๋ก๊ทธ์ธ ๋ ์ํ์ธ์ง) ํ๋ณ ๊ฐ๋ฅ {% endif %}
10. {% if not user.is_authenticated %}๋ ์ธ์ฆ๋์ง ์์(๋ก๊ทธ์ธ ์๋) ์ ์ ์๊ฒ๋ง ๋์ฐ๊ธฐ {% endif %}
[ User ํ์ฅ ]
11. models.py์์ ์ ์ ํ์ฅ
from django.db import models
from django.contrib.auth.models import AbstractUser
class CustomUser(AbstractUser):
nickname = models.CharField(max_length=100)
university = models.CharField(max_length=50)
location = models.CharField(max_length=200)
12. settings.py์์ ์ธ์ฆ ๋ฐ์ ์ ์ ๋ชจ๋ธ์ ์ด ๋ชจ๋ธ๋ก ์ฌ์ฉํ๊ฒ ๋ค๋ ๊ฒ ์ ์
AUTH_USER_MODEL = 'account.CustomUser' #์ธ์ฆํ๋ ์ ์ ๋ชจ๋ธ๋ก ์ฌ์ฉํ๊ฒ ๋ค๊ณ ์ ์ธ
13. python manage.py makemigration
14. settings.py์ INSTALLED_APPS๋ด 'django.contrib.admin' ์ฃผ์์ฒ๋ฆฌ
15. urls.py์ from django.contrib import admin, path('admin/'~~) ์ฃผ์์ฒ๋ฆฌ
16. python manage.py migrate
17. ์์ ์ฃผ์์ฒ๋ฆฌ ํ๋ ๊ฒ๋ค ๋ค์ ํด์
18. account์ฑ์์ forms.py ์์ฑ
from django.contrib.auth.forms import UserCreationForm
from .models import CustomUser
class RegisterForm(UserCreationForm):
class Meta:
model = CustomUser
fields = ['username', 'password1', 'password2', 'nickname', 'location', 'university']
#password ๋๊ฐ : ํจ์ค์๋ ํ์ธ ์ํด ๋๋ฒ ์
๋ ฅํจ
19. views.py์์ RegisterForm ์ฌ์ฉํ๋๋ก ๋ณ๊ฒฝ
from django.shortcuts import render, redirect
from django.contrib.auth.forms import AuthenticationForm
from django.contrib.auth import authenticate, login, logout
from .forms import RegisterForm
def register_view(request):
if request.method == 'POST': #request method๊ฐ post๋ฉด form์ ๋ฐ์
form = RegisterForm(request.POST)
if form.is_valid(): #ํผ ๋ฐ์ ํ ์ ํจ์ฑ ๊ฒ์ฌ
user = form.save() #ํผ์ ์ ์ฅ, ํผ ์์ ์ ์ด์ฃผ๋ ์ ๋ณด ์ธ์ ํ์ํ ์ ๋ณด ์์, commit ์์ฑx
login(request, user)
return redirect("home")
else:
form = RegisterForm()
return render(request, 'signup.html', {'form':form})
20. admin.py์ ๋ชจ๋ธ ๋ฑ๋ก
from django.contrib import admin
from .models import CustomUser
# Register your models here.
admin.site.register(CustomUser)
'web > likelion' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[Django] null=True / blank=True (0) | 2021.07.06 |
---|---|
[Django] pip freeze '@ file:///' ํํ๋ก ๋์ฌ ๋ ํด๊ฒฐ (0) | 2021.06.16 |
[Django] ๊ณตํต๋ template ์์ (0) | 2021.05.18 |
[Django] vscode ๊ฐ์ํ๊ฒฝ ์์ฑ ๋ฐ django ์ค์น ๋ช ๋ น์ด (0) | 2021.05.15 |