meong_j
기록하는 습관.
meong_j
전체 방문자
오늘
어제
  • 분류 전체보기 (176)
    • 개인 공부 정리 (0)
    • 서버 운영 (37)
      • Linux (36)
    • Frontend (11)
      • Vue.js (10)
    • Backend (70)
      • Java (4)
      • Python (22)
      • Django (38)
      • Spring (6)
    • Database (5)
      • Oracle (4)
      • MySQL (1)
      • MariaDB (0)
    • Android (14)
      • Kotlin (6)
    • 배포 (9)
      • Docker (8)
      • AWS (1)
    • IT_study (29)
      • Coding test (17)
      • 알고리즘 (5)
      • 스터디 (6)

블로그 메뉴

  • 홈
  • 태그
  • 방명록
  • github

인기 글

반응형

태그

  • 안드로이드adaptor
  • Kotlin
  • django
  • 배포인프라
  • 코틀린자료형
  • 개발자도서
  • 리눅스방화벽
  • dockersecret
  • router-link
  • docker
  • SASS Variables
  • 리눅스인증
  • cpu사용률
  • 이차원배열정렬
  • 테크커리어
  • dp #알고리즘
  • Proxy
  • 중첩라우트
  • DHCP
  • gabagecollecter

최근 댓글

최근 글

250x250
hELLO · Designed By 정상우.
meong_j

기록하는 습관.

django 20. Profileapp 마무리
Backend/Django

django 20. Profileapp 마무리

2021. 10. 6. 18:33
728x90
반응형
Profileapp 추가된 리스트 정리
  • Profile 내용 update
  • image 출력 및 profile 페이지 수정
  • decorator 설정

 

 

프로필 Detail 화면 수정

{% extends 'base.html' %}
{% load bootstrap4 %}

{% block content %}

<div>
    <div style="text-align: center; max-width: 500px; margin: 4rem auto;">

        <img src="{{ target_user.profile.image.url }}" alt="" style="height: 12rem; width: 12rem; border-radius: 20rem; margin-bottom: 2rem;">


        {% if target_user.profile %}
         <h2 style="font-family: NanumBarunpenB">
            {{ target_user.profile.nickname }}
             <a href=" {% url 'profileapp:update' pk=target_user.profile.pk %}">
                 edit
             </a>
        </h2>
        {% else %}
        <a href="{% url 'profileapp:create' %}">
            <h2 style="font-family: NanumBarunpenB">
                Create Profile
            </h2>
        </a>
        {% endif %}

        <h5 style="margin-bottom: 4rem">
            {{ target_user.profile.message }}
        </h5>

        {% if target_user == user %}
        <a href="{% url 'accountapp:update' pk=user.pk %}">
            <p>
                Change Info
            </p>
        </a>
        {% endif %}

        {% if target_user == user %}
        <a href="{% url 'accountapp:delete' pk=user.pk %}">
            <p>
                Quit
            </p>
        </a>
        {% endif %}

    </div>

</div>

{% endblock %}
  • img 파일 추가 및 css 변경

 

  • 수정한 Profile 페이지 화면

 

 

Profile update 구현

views.py

class ProfileUpdateView(UpdateView):
    model = Profile
    context_object_name = 'target_profile'
    form_class = ProfileCreationForm
    success_url = reverse_lazy('accountapp:hello_world')
    template_name = 'profileapp/update.html'
  • 내가 만든 profileCreationForm으로 form 생성
  • update.html로 이동

 

urls.py

from django.urls import path

from profileapp.views import ProfileUpdateView

app_name = 'profileapp'


urlpatterns = [
    path('update/<int:pk>', ProfileUpdateView.as_view(), name='update'),
]
  • profileupdateview로 path 설정

 

update.html

{% extends 'base.html' %}
{% load bootstrap4 %}

{% block content %}

<div style="text-align:center;  max-width: 500px; margin: 4rem auto">
    <div class="mb-4">
        <h4>Update Profile</h4>
    </div>
    <form action="{% url 'profileapp:update' pk=target_profile.pk %}"  method="post" enctype="multipart/form-data">
        {% csrf_token %}
        {% bootstrap_form form %}
        <input type="submit" class="btn btn-dark rounded-pill col-6 mt-3">
    </form>
</div>


{% endblock %}
  • 프로필 업데이트 페이지 화면 구현

 

  • profile 수정 페이지 화면

  • 정보 수정후 profile 페이지 화면

 

 

Decorator 추가

from django.http import HttpResponseForbidden

from profileapp.models import Profile


def profile_ownership_required(func):
    def decorated(request, *args, **kwargs):
        profile = Profile.objects.get(pk=kwargs['pk'])
        if not profile == request.user:
            return HttpResponseForbidden()
        return func(request, *args, **kwargs)
    return decorated
  • 자신이외의 다른 user페이지 접근 금지 decorator 설정

 

 

@method_decorator(profile_ownership_required, 'get')
@method_decorator(profile_ownership_required, 'post')
class ProfileUpdateView(UpdateView):
    model = Profile
    context_object_name = 'target_profile'
    form_class = ProfileCreationForm
    success_url = reverse_lazy('accountapp:hello_world')
    template_name = 'profileapp/update.html'
  • views.py의 생성한 클래스에 method_decorator 지정

 

 

반응형

'Backend > Django' 카테고리의 다른 글

django 22. MagicGrid 소개 및 Articleapp 시작  (0) 2021.10.10
django 21. get_success_url 함수 그리고 리팩토링  (0) 2021.10.06
django 19. Profileapp 구현 시작  (0) 2021.10.06
django 18. Profileapp 시작 그리고 ModelForm  (0) 2021.10.06
django 17. superuser, media 관련 설정  (0) 2021.10.06
    'Backend/Django' 카테고리의 다른 글
    • django 22. MagicGrid 소개 및 Articleapp 시작
    • django 21. get_success_url 함수 그리고 리팩토링
    • django 19. Profileapp 구현 시작
    • django 18. Profileapp 시작 그리고 ModelForm
    meong_j
    meong_j
    #it #개발일기 #개발공부 #개발자 #백앤드 #생각정리 #시간은 실력에 비례한다 #뭐든지 꾸준히 열심히 #오늘의 내가 내일의 나를 만든다

    티스토리툴바