ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • Django 초간단 게시판 9. password change / reset
    Python/초간단 장고 Django 2021. 5. 28. 16:19
    반응형

    password change : http://127.0.0.1:8000/accounts/password_change/

    당연한 이야기지만 로그인 상태에서만 바꿀 수 있다. 

     

    password reset : http://127.0.0.1:8000/accounts/password_reset/

    장고는 이메일로 패스워드 리셋을 처리 한다. 

     

    templates/registration/password_reset_form.html
    위 파일을 편집하면 폼을 바꿀 수 있다. 

    <!--templates/registration/password_reset_form.html-->
    <!DOCTYPE html>
    <html lang="ko">
    <head>
        <meta charset="UTF-8">
        <title>Title</title>
    </head>
    <body>
      <h1>Forgot your password?</h1>
      <p>Enter your email address below, and we'll email instructions for setting a new one.</p>
    
      <form method="POST">
        {% csrf_token %}
        {{ form.as_p }}
        <input type="submit" value="Send me instructions!">
      </form>
    </body>
    </html>

    templates/registration/password_reset_done.html
    도 적당히 바꿔주자. 

    Password reset sent

    We’ve emailed you instructions for setting your password, if an account exists with the email you entered. You should receive them shortly.

    If you don’t receive an email, please make sure you’ve entered the address you registered with, and check your spam folder.

    이런 내용이 들어가면... 될 것 같다. 

     

    특별히 설정을 하지 않았기 때문에 버튼을 눌러도 이런 에러메시지만 볼 수 있다. 

     

    장고는 개발용으로 파일로 이메일을 저장할 수 있게 해준다.
    config/settings.py에 다음을 추가한다. 

    # config/settings.py
    EMAIL_BACKEND = "django.core.mail.backends.filebased.EmailBackend"
    EMAIL_FILE_PATH = BASE_DIR / 'sent_emails'

     

    이메일 전송에 성공했다.

     

     

    위에서 지장한 sent_emails라는 폴더에 보면 log 파일들이 보인다. 

    Content-Type: text/plain; charset="utf-8"
    MIME-Version: 1.0
    Content-Transfer-Encoding: 8bit
    Subject: Password reset on 127.0.0.1:8880
    From: webmaster@localhost
    To: test@test.com
    Date: Fri, 28 May 2021 07:54:31 -0000
    Message-ID: <162218847120.20964.5321806074043105070@SSSM-SV>
    
    
    You're receiving this email because you requested a password reset for your user account at 127.0.0.1:8880.
    
    Please go to the following page and choose a new password:
    
    http://127.0.0.1:8880/accounts/reset/Mw/andayv-68a4a6e8512aebe479c9795f492bd8f0/
    
    Your username, in case you’ve forgotten: testtest
    
    Thanks for using our site!
    
    The 127.0.0.1:8880 team
    
    
    
    -------------------------------------------------------------------------------
    

     

    여기 링크에 접속해 보면 다음 화면이 보인다. 

    templates/registration/password_reset_confirm.html 를 만들자. 

    <!--templates/registration/password_reset_confirm.html-->
    <!DOCTYPE html>
    <html lang="ko">
    <head>
        <meta charset="UTF-8">
        <title>Title</title>
    </head>
    <body>
    
    {% if validlink %}
    
    <h1>Set a new password!</h1>
    <form method="POST">
      {% csrf_token %}
      {{ form.as_p }}
      <input type="submit" value="Change my password">
    </form>
    
    {% else %}
    
    <p>The password reset link was invalid, possibly because it has already been used.  Please request a new password reset.</p>
    
    {% endif %}
    </body>
    </html>

    완전히 끝나면 이런 화면이.... 

    password_reset_complete.html 을 작성하자. 

    <!--templates/registration/password_reset_complete.html-->
    <!DOCTYPE html>
    <html lang="ko">
    <head>
      <meta charset="UTF-8">
      <title>Title</title>
    </head>
    <body>
      <h1>Password reset complete</h1>
      <p>Your new password has been set. You can log in now on the <a href="{% url 'login' %}">log in page</a>.</p>
    </body>
    </html>

     

    참고 : https://learndjango.com/tutorials/django-password-reset-tutorial

    반응형
Designed by Tistory.