웹 프로그래밍

[pinterest clone (24)] profileapp update view

728x90
반응형

1

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'

2

urls.py

from django.urls import path

from profileapp.views import ProfileCreateView, ProfileUpdateView

app_name = 'profileapp'

urlpatterns = [
    path('create/', ProfileCreateView.as_view(), name='create'),
    path('update/<int:pk>', ProfileUpdateView.as_view(), name='update'),
]

3

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 %}

4

accountapp/detail.html

{% extends 'base.html' %}

{% block content %}

<div>
    <div style="text-align: center; max-width: 500px; margin: 4rem auto;">
        <p>
            {{ target_user.date_joined }}
        </p>

        <img src="{{ target_user.profile.image.url }}" alt="">
        {% if target_user.profile %}
        <h2 style="font-family: 'NanumSquareB'">
            {{ 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: 'NanumSquareB'">
                Create Profile
            </h2>
        </a>
        {% endif %}
        {% if target_user == user %}
        <a href="{% url 'accountapp:update' pk=user.pk %}">
            <p>
                Change Info
            </p>
        </a>
        <a href="{% url 'accountapp:delete' pk=user.pk %}">
            <p>
                Quit
            </p>
        </a>
        {% endif %}
    </div>
</div>

{% endblock %}

update는 작동하고, <img src="{{ target_user.profile.image.url }}" alt=""> 넣었는데 이미지가 나타나지않는다.

=> 라우팅을 안해서 그렇다.

 

platypus/urls.py

from django.conf import settings
from django.conf.urls.static import static
from django.contrib import admin
from django.urls import path, include

urlpatterns = [
    path('admin/', admin.site.urls),
    path('accounts/', include('accountapp.urls')),
    path('profiles/', include('profileapp.urls')),
] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)

 

5

데코레이터 추가

profileapp/decorators.py

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.user == request.user:
            return HttpResponseForbidden()
        return func(request, *args, **kwargs)
    return decorated

views.py

@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'

 

duckracoon.tistory.com/52

 

[pinterest clone (20)] Authentication

views.py - def hello_world def hello_world(request): if request.user.is_authenticated: if request.method == "POST": temp = request.POST.get('hello_world_input') new_hello_world = HelloWorld() new_he..

duckracoon.tistory.com

* 데코레이션 추가의 목적은 여기서와 같이 개인 프로필의 인증과정을 위함이다. 무분별한 접속과 수정방지. 남들이 남의 것을 업데이트하면 안되니까.

728x90
반응형