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

๐Ÿ’ชBackend/Django

Django๋กœ Pinterest ๋”ฐ๋ผ๋งŒ๋“ค๊ธฐ : ์„น์…˜ 2. Accountapp implementation (21~26๊ฐ•)

์„น์…˜2์—์„œ๋Š” accounapp์˜ crud์™€ login/logout์„ ๊ตฌํ˜„

 

#21 CreateView๋ฅผ ํ†ตํ•œ ํšŒ์›๊ฐ€์ž… ๊ตฌํ˜„

ํ˜„์žฌ ๊ฐ•์˜๊นŒ์ง€๋Š” ์œ„ ๊ตฌ์กฐ๋„์—์„œ ์žฅ๊ณ  ๋ถ€๋ถ„๋งŒ์„ ๊ตฌ์ถ•ํ•˜๊ณ  ์žˆ์Œ

class AccountCreateView(CreateView):
    model = User
    form_class = UserCreationForm
    success_url = reverse_lazy('accountapp:hello_world')
    template_name = 'accountapp/create.html'

์ด๋Ÿฐ์‹์œผ๋กœ create view์ด์šฉํ•ด์„œ ํšŒ์›๊ฐ€์ž… ๊ตฌ์ถ•

 

 


 

 

 

#22 Login/Logout ๊ตฌํ˜„

login๊ณผ logout view๋„ ์žฅ๊ณ ์—์„œ ๊ธฐ๋ณธ์œผ๋กœ ์ œ๊ณตํ•ด์คŒ

 

view ์‚ฌ์šฉ์œผ๋กœ url ์ˆ˜์ •

path('login/',LoginView.as_view(template_name='accountapp/login.html'), name='login'),
path('logout/',LogoutView.as_view(), name='logout'),

์ด๋Ÿฐ ์‹์œผ๋กœ url์— ๋ฐ”๋กœ ์ถ”๊ฐ€

login.html ๊ตฌํ˜„ํ•ด์ฃผ๋ฉด ๋

 

 

 

header.html ์ˆ˜์ •

 

๊ตฌ์กฐ๊ฐ€ ์œ„์™€ ๊ฐ™์•„์„œ ๋กœ๊ทธ์ธ ํ•˜๊ฒŒ ๋˜๋ฉด accounts/profile๋กœ ์ด๋™ํ•˜๊ฒŒ๋˜๋Š”๋ฐ ์ด๋ ‡๊ฒŒ ์ž๋™์œผ๋กœ ๋‹ค๋ฅธ ์ฃผ์†Œ๋กœ ๊ฐ€๊ฒŒ ๋˜๋Š” ๊ฒƒ์„ ๋ง‰๊ธฐ ์œ„ํ•ด ์ˆ˜์ • ํ•„์š”

 

 

{% if not user.is_authenticated %}
            <a href="{% url 'accountapp:login' %}?next={{request.path}}">
                <span>login</span>
            </a>
            {% else %}
            <a href="% url 'accountapp:logout' %}?next={{request.path}}">
                <span>logout</span>
            </a>
            {% endif %}

 

์ด๋Ÿฐ์‹์œผ๋กœ next์ธ์ž๋„ ์ „๋‹ฌํ•ด์ฃผ๊ณ  ๋กœ๊ทธ์ธ/๋กœ๊ทธ์•„์›ƒ ํŽ˜์ด์ง€๋กœ ์ด๋™ํ•˜๋Š” span๋„ ๋งŒ๋“ค์–ด์คŒ

์›๋ž˜์žˆ์—ˆ๋˜ url์ด next๋กœ ๋„˜์–ด๊ฐ€๋ฉด์„œ hello_world route๋กœ ๋‹ค์‹œ ๋Œ์•„๊ฐ

 

 

 

๊ทธ๋Ÿฌ๋‚˜, span๋ง๊ณ  url์„ login์œผ๋กœ ์ง์ ‘ ์น˜๊ณ  ๋“ค์–ด๊ฐ€์„œ ๋กœ๊ทธ์ธํ•˜๋Š” ๊ฒฝ์šฐ ์•„์ง ์œ„์™€ ๊ฐ™์€ ์ƒํ™ฉ ๋ฐœ์ƒ

⇒ login_redirect_url ์„ค์ • ํ•„์š”

LOGIN_REDIRECT_URL = reverse_lazy('accountapp:hello_world')
LOGOUT_REDIRECT_URL = reverse_lazy('accountapp:login')

์œ„์™€ ๊ฐ™์ด pragmatic/urls.py ์—์„œ url ์„ค์ •ํ•ด์คŒ

 

 


 

 

#23 Bootstrap์„ ์ด์šฉํ•œ Form ๋””์ž์ธ ์ •๋ฆฌ

bootstrap์„ ์ด์šฉํ•˜์—ฌ form ๋””์ž์ธ์„ ์ผ๊ด„์ ์œผ๋กœ ์ˆ˜์ •๊ฐ€๋Šฅ

https://django-bootstrap4.readthedocs.io/en/latest/index.html

 

django-bootstrap4 23.4 documentation

Next Installation

django-bootstrap4.readthedocs.io

์œ„์˜ ๋งํฌ ์ฐธ๊ณ 

 

 

 

install ํ•˜๊ณ  settings.py์— ๋ช…์‹œํ•ด์ฃผ๊ธฐ

{% load bootstrap4 %}

{% bootstrap_form form %}

์œ„์™€ ๊ฐ™์€์‹์œผ๋กœ ๋ถˆ๋Ÿฌ์™€์„œ ์‚ฌ์šฉํ•˜๋ฉด ๋จ

 

 

 

๋„ค์ด๋ฒ„์—์„œ ํฐํŠธ ๋ฐ›์•„์™€์„œ ์ถ”๊ฐ€๋กœ ์„ค์ •ํ•ด์คŒ

 

pragmatic/static ์— fonts ๊ฒฝ๋กœ ์ถ”๊ฐ€ํ•ด์„œ ๊ทธ fonts ํŒŒ์ผ ์•ˆ์— ๋ฐ›์•„์˜จ ํฐํŠธํŒŒ์ผ๋“ค ์ €์žฅํ•ด์คŒ ๊ทธ๋ฆฌ๊ณ  head.html์—

<style>
        @font-face{
            font-family:'NanumGothic';
            src:local('NanumBarunGothic-YetHangul'),
            url("{% static 'fonts/NanumBarunGothic-YetHangul.otf' %}") format("opentype");
        }
    </style>

์œ„์™€ ๊ฐ™์€ ์‹์œผ๋กœ ๋„ฃ์–ด์ค€ ๋’ค, ์‚ฌ์šฉํ•˜๊ณ ์ž ํ•˜๋Š” ์œ„์น˜์— ํฐํŠธ ์„ค์ •

 

 


 

 

#24 DetailView๋ฅผ ์ด์šฉํ•œ ๊ฐœ์ธ ํŽ˜์ด์ง€ ๊ตฌํ˜„

createView ๋งŒ๋“ค์—ˆ๋˜ ๊ฒƒ์ฒ˜๋Ÿผ Detail view ์ƒ์„ฑ

urls.py

path('detail/<int:pk>',AccountDetailView.as_view(), name='detail'),

์ •๋ณด๋ฅผ ์ƒ์„ฑํ–ˆ๋˜ create์™€ ๋‹ค๋ฅด๊ฒŒ detail์€ ์ •๋ณด๋ฅผ ์—ด๋žŒํ•˜๋Š” ๊ฒƒ

์–ด๋–ค ์œ ์ €์˜ ์ •๋ณด๋ฅผ ์—ด๋žŒํ•  ์ง€ ์•Œ๊ธฐ ์œ„ํ•ด์„œ int:pk ํ•ด์คŒ (primary key)

 

 

header.html

<a href="{% url 'accountapp:detail' pk=user.pk %}">
                <span>MyPage</span>
</a>

 

๋กœ๊ทธ์ธ ๋˜์–ด์žˆ๋Š” ์ƒํ™ฉ์—์„œ MyPage๋กœ ๋„˜์–ด๊ฐˆ ์ˆ˜ ์žˆ๋Š” span์„ ๋งŒ๋“ค์–ด์คŒ

์ด๋•Œ, pk ๊ฐ’์„ ์ „๋‹ฌํ•ด์ฃผ์–ด์•ผํ•˜๊ธฐ ๋–„๋ฌธ์— ๋กœ๊ทธ์ธ๋œ ์œ ์ €์˜ pk๋ฅผ pk=user.pk๋กœ ๋„˜๊ฒจ์คŒ

 

๋กœ๊ทธ์ธ ํ›„ mypage ๋ˆŒ๋Ÿฌ๋ณด๋ฉด ์œ„์ฒ˜๋Ÿผ ๊ฐ€์ž…๋‚ ์งœ์™€ ์ด๋ฆ„ ํ™•์ธ ๊ฐ€๋Šฅ

 

 

 


 

 

#25 UpdateView๋ฅผ ์ด์šฉํ•œ ๋น„๋ฐ€๋ฒˆํ˜ธ ๋ณ€๊ฒฝ ๊ตฌํ˜„

 

updateview ์ž‘์„ฑ

updateview๋„ createview์™€ ์œ ์‚ฌํ•˜๊ฒŒ ์ž‘์„ฑ ๊ฐ€๋Šฅ

 

update.html๋„ ์ž‘์„ฑํ•ด์ค€ ๋’ค, detail.html์—์„œ update๋กœ ๋„˜์–ด๊ฐˆ ์ˆ˜ ์žˆ๋Š” ๋งํฌ๋ฅผ ํ•˜๋‚˜ ์ƒ์„ฑํ•ด์ฃผ๋ฉด,

์•„๋ž˜์ฒ˜๋Ÿผ change info๊ฐ€ ๋งŒ๋“ค์–ด์ง„ ๊ฒƒ์„ ๋ณผ ์ˆ˜ ์žˆ์Œ.

(์ด๋•Œ ๋งํฌ๊ฐ€ ๋ณด์ด๋Š” ์กฐ๊ฑด target_user==user์ธ ๊ฒฝ์šฐ)

Change Info ๋ฒ„ํŠผ ํด๋ฆญ ์‹œ ๋‚˜ํƒ€๋‚˜๋Š” ์ˆ˜์ •ํ™”๋ฉด

 

 

 

 

AccountUpdateForm ์ž‘์„ฑ

 

html ์ž‘์„ฑํ•  ๋•Œ usercreationform์„ ๊ทธ๋Œ€๋กœ ๋ฐ›์•„์™€์„œ ์‚ฌ์šฉํ–ˆ๋”๋‹ˆ username(id)๋„ ์ˆ˜์ • ๊ฐ€๋Šฅํ•ด์„œ ์ด๋ฅผ ์ƒ์†๋ฐ›๋Š” ์ƒˆ๋กœ์šด form ์ž‘์„ฑ

from django.contrib.auth.forms import UserCreationForm

class AccountUpdateForm(UserCreationForm):
    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self.fields['username'].disabled=True

 

์œ„ ์ฒ˜๋Ÿผ disabled=True๋กœ ์ปค์Šคํ„ฐ๋งˆ์ด์ง•ํ•˜๋ฉด,

username ๋น„ํ™œ์„ฑํ™” ๋จ!!

 

 

 


 

 

 

#26 DeleteView ๊ธฐ๋ฐ˜ ํšŒ์›ํƒˆํ‡ด ๊ตฌํ˜„

 

deleteview ์ž‘์„ฑ

deleteview๋„ ๋งˆ์ฐฌ๊ฐ€์ง€๋กœ createview ์ž‘์„ฑํ–ˆ๋˜๊ฒƒ์ฒ˜๋Ÿผ ์ƒ์„ฑํ•ด์คŒ

updateview ๋ณด์ด๊ฒŒ ํ–ˆ๋˜ ๊ฒƒ์ฒ˜๋Ÿผ quit ์œผ๋กœ ๊ฐ€๋Š” ๋งํฌ ์ถ”๊ฐ€ํ•ด์ฃผ๊ณ  quit ๋“ค์–ด๊ฐ€๋ณด๋ฉด ์•„๋ž˜์™€ ๊ฐ™์ด ๋œธ

 

 

์ œ์ถœ ๋ฒ„ํŠผ ๋ˆ„๋ฅด๋ฉด ํ•ด๋‹น ๊ณ„์ • ์‚ญ์ œ

 

 

 


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

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 ์‹ค์ „ ๊ฐœ๋ฐœ์„ ํ†ตํ•ด ํ•œ ๋‹จ๊ณ„ ๋†’์€ ๊ฐœ๋ฐœ์ž๊ฐ€ ๋˜