본문 바로가기

Computer Science/Django_pinterest

210821 장고 25커밋, UpdateView를 이용한 비밀번호 변경 구현

forms.py

from django.contrib.auth.forms import UserCreationForm

class AccountUpdateForm(UserCreationForm):
    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)

        self.fields['username'].disabled = True

# if this code does not existing it can be changed the ID

 

detail.py
{% extends 'base.html' %}

{% block content %}

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

            {% if target_user == user %}
            <!--if it is correct show below link            -->
            <!--going to url below             -->
            <a href="{% url 'accountapp:update' pk=user.pk %}">
                <p>
                    Change Info
                </p>
            </a>
            {% endif %}
        </div>

    </div>
{% endblock %}
<!--인스타 느낌으로 생각 , 접속한 유저의 정보가아닌 다른사람의 정보를 보기위해해 views에서 target유저로 변경->
다른 사람이 제 페이지를 오더라도 정상적으로 저희 페이지를 볼 수 있다.

 

views.py
from django.contrib.auth.forms import UserCreationForm
from django.contrib.auth.models import User
from django.http import HttpResponse, HttpResponseRedirect
from django.shortcuts import render

# Create your views here.

#view 단의 완성
from django.urls import reverse, reverse_lazy
from django.views.generic import CreateView, DetailView, UpdateView

from accountapp.models import HelloWorld
from accountapp.templates.accountapp.forms import AccountUpdateForm


def hello_world(request):

    if request.method == "POST":
        # 포스트가 완료한이후에 겟으로 되돌아가서 남아있는 글들을 지울 수 있게한다.
        temp = request.POST.get("hello_world_input")

        new_hello_world = HelloWorld()
        new_hello_world.text = temp
        new_hello_world.save()

        hello_world_list = HelloWorld.objects.all() #헬로월드에 모든 데이터를 다 긁어온다.
        return HttpResponseRedirect(reverse('accountapp:hello_world'))
    # 포스트가 완료한이후에 겟으로 되돌아가서 남아있는 글들을 지울 수 있게한다. redirect
    # URL에서 ACCOUNT를 가져온다. 해당 경로를 만들어줄 함수를 쓴다
    else:
        hello_world_list = HelloWorld.objects.all()
        return render(request, 'accountapp/hello_world.html',
                      context={'hello_world_list': hello_world_list})
#view는 만들었고 이제 route해줘야한다.
#특정 주소를 만들어 주는 작업이 필요하다.

class AccountCreateView(CreateView):
    model = User
    form_class = UserCreationForm
    success_url = reverse_lazy('accountapp:hello_world') #여기서 reverse를 사용불가 클래스형
    #reverse는 함수형에서 사용한다.
    template_name = 'accountapp/create.html'

class AccountDetailView(DetailView):
    model = User
    context_object_name = 'target_user'
    template_name = 'accountapp/detail.html'

class AccountUpdateView(UpdateView):
    model = User
    form_class = AccountUpdateForm
    success_url = reverse_lazy('accountapp:hello_world') #여기서 reverse를 사용불가 클래스형
    #reverse는 함수형에서 사용한다.
    template_name = 'accountapp/update.html'