hyeonga_code

파이선 웹구축_장고_21_계정 비밀번호 변경 본문

Python_Django

파이선 웹구축_장고_21_계정 비밀번호 변경

hyeonga 2023. 6. 13. 05:59
반응형

- 계정 비밀번호 변경

1. 업데이트 페이지에 버튼 생성 <templates> > <adm> > 'update.html'

'update.html'

=====

1
2
3
4
5
6
<h1><b>UPDATE PAGE</b></h1>
<br><br>
 
<form method="post">
    <a href="{% url 'chpass' %}"><button type="button">Change Password</button></a>
</form>
cs

 

 

 

2. 경로 설정 <adm> > 'urls.py'

'urls.py'

=====

1
2
3
4
5
6
from django.urls import path, include
from . import views
 
urlpatterns = [
    path('chpass/', views.chpass, name="chpass")
]
cs

 

 

 

 

3. 함수 설정 <adm> > 'views.py'

'views.py'

=====

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
from django.shortcuts import render, redirect
from django.contrib.auth import authenticate, login, logout
from .models import User
from django.contrib.auth.hashers import check_password
 
def chpass(request):
    if request.user.is_anonymous:
        return redirect("login")
 
    if request.method == "POST":
        u = request.user
        cp = request.POST.get("cpass")
        if check_password(cp, u.password):
            np = request.POST.get("npass")
            u.set_password(np)
            u.save()
            return redirect("login")
    return render(request, "adm/chpass.html")
cs

 

 

 

4. 비밀번호 변경 페이지 작성 <templates> > <adm> > 'chpass.html'

'chpass.html'

=====

1
2
3
4
5
6
7
8
9
<h1><b>PASSWORD CHANGE</b></h1>
 
<form method="post">
    {% csrf_token %}
    <input type="password" name="cpass" placeholder="INPUT CURRENT PASSWORD" size="50"><br><br>
    <input type="password" name="npass" placeholder="INPUT NEW PASSWORD" size="50"><br><br>
    <button>Change</button>
    <a href="{% url 'update' %}"><button type="button">Cancle</button></a>
</form>
cs

 

 

반응형