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'
* 데코레이션 추가의 목적은 여기서와 같이 개인 프로필의 인증과정을 위함이다. 무분별한 접속과 수정방지. 남들이 남의 것을 업데이트하면 안되니까.
728x90
반응형
'웹 프로그래밍' 카테고리의 다른 글
[pinterest clone (26)] Articleapp 화면구성 (MagicGrid) (0) | 2021.05.01 |
---|---|
[pinterest clone (25)] get_success_url 함수와 리팩토링 (0) | 2021.04.30 |
[pinterest clone (23)] profileapp create view (0) | 2021.04.30 |
[pinterest clone (22)] ModelForm (0) | 2021.04.30 |
[pinterest clone (21)] superuser, media 관련설정 (0) | 2021.04.30 |