📢 공지사항
home

장고3주차

21강

django container
Accountapp
•
view 상속 : django.views.generic
•
reverse : def
•
reverse_lazy : class

22강

•
Login/Logout View
◦
Login
▪
path('login/', LoginView.as_view(template_name='accountapp/login.html'), name='login'),
◦
Logout
▪
path('logout/', LogoutView, name='logout'),
•
Redirect Mechanism
◦
next → LOGIN_REDIRECT_URL → Default
◦
settings.py
LOGIN_REDIRECT_URL = reverse_lazy('accountapp:hello_world') LOGOUT_REDIRECT_URL = reverse_lazy('accountapp:login')
Python
복사

23강

•
bootstrap 스타일링 정리
◦
bootstrap 설치 → {{ form }} 대신 {% bootstrap_form form %}사용
class="btn btn-dark rounded-pill col-6 mt-3
Python
복사
col : parent 너비의 비율 (12=100%), mt(margin top)
•
글꼴
◦
static 파일에 fonts , fonts 에 사용할 폰트(.otf) 넣기
◦
head.html
<style> @font-face { font-family: 'NanumSquareR'; src: local('NanumSquareR'), url("{% static 'fonts/NanumSquareR.otf' %}") format("opentype"); } @font-face { font-family: 'NanumSquareEB'; src: local('NanumSquareEB'), url("{% static 'fonts/NanumSquareEB.otf' %}") format("opentype"); } @font-face { font-family: 'NanumSquareB'; src: local('NanumSquareB'), url("{% static 'fonts/NanumSquareB.otf' %}") format("opentype"); } @font-face { font-family: 'NanumSquareL'; src: local('NanumSquareL'), url("{% static 'fonts/NanumSquareL.otf' %}") format("opentype"); } </style>
HTML
복사

24강

•
Detail View (Read View)
◦
Detail 을 보기 위해서는 primary key 필요
class AccountDetailView(DetailView): model = User context_object_name = 'target_user' template_name = 'accountapp/detail.html'
Python
복사
<div> <div style="text-align:center; max-width:500px; margin: 4rem auto"> <p> {{ target_user.date_joined }} </p> <h2 style="font-family: 'NanumSquareB';"> {{ target_user.username }} </h2> </div> </div>
HTML
복사

25강

•
Update View (Change info)
◦
Create View와 유사
•
ID 수정 비활성화
◦
accountapp → forms.py
from django.contrib.auth.forms import UserCreationForm class AccountUpdateForm(UserCreationForm): def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs) self.fields['username'].disabled = True
Python
복사

26강

•
Delete View
◦
views.py
class AccountDeleteView(DeleteView): model = User success_url = reverse_lazy('accountapp:login') template_name = 'accountapp/delete.html'
Python
복사

27강

•
Authentication

28강

•
Decorator
◦
함수를 꾸며주는 역할
login 여부: login_required
lognin user 와 현재 페이지 접근 user 일치
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
Plain Text
복사
@login_required
Python
복사
decorator 사용시 @
has_ownership = [account_ownership_required, login_required]
Python
복사
리스트 가능

29강

•
Super User 생성
◦
admin
•
media 관련

30강

•
profile
◦
Account : Profile
◦
Profile
▪
Image, Nickname, Message
▪
Delete, Detail View
▪
user = models.OneToOneField(User, on_delete=models.CASCADE)
CASCADE : user 가 삭제될 때 profile도 같이 사라짐
•
Form
◦
Model Form
▪
profile form X → Model Form을 활용하여 적절한 Form 생성
from django.forms import ModelForm from profileapp.models import Profile class ProfileCreationForm(ModelForm): class Meta: model = Profile fields = ['image', 'nickname', 'message']
Python
복사