Backend/Django

django 20. Profileapp 마무리

meong_j 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 지정

 

 

반응형