環境
Windows11
Python 3.10.5
Django 4.2.7
経緯
Django+Pythonで学習がてらBlog作成中、パスワードリセット機能もつけたいと思い。
views.py
class PasswordReset(PasswordResetView):
# パスワードリセット用URLの送信ページ
success_url = reverse_lazy("blogapp:password_reset_done")
# パスワード変更URL付きメールのカスタマイズ
email_template_name = "blogapp/password_reset_email.html"
template_name = "blogapp/password_reset_form.html"
class PasswordResetDone(PasswordResetDoneView):
# パスワード変更用URL送信完了ページ
template_name = "blogapp/password_reset_done.html"
class PasswordResetConfirm(PasswordResetConfirmView):
# 新パスワード入力用ページ
success_url = reverse_lazy("blogapp:password_reset_complete")
template_name = "blogapp/password_reset_confirm.html"
class PasswordResetComplete(PasswordResetCompleteView):
# 新パスワード設定完了ページ
template_name = "blogapp/password_reset_complete.html"
urls.py
app_name = "blogapp"
urlpatterns = [
path(
"password_reset/",
PasswordReset.as_view(),
name="password_reset",
),
path(
"password_reset/done/",
PasswordResetDone.as_view(),
name="password_reset_done",
),
path(
"reset/<uidb64>/<token>/",
PasswordResetConfirm.as_view(),
name="password_reset_confirm",
),
path(
"reset/done/",
PasswordResetComplete.as_view(),
name="password_reset_complete",
)
]
password_reset_form.html
パスワードリセット用メール送信ページ
{% extends "blogapp/base.html" %}
{% load static %}
{% block content %}
<h1>Reset password</h1>
<form method="POST">
{% csrf_token %}
{{ form.as_p }}
<p><input type="submit" value="Send Mail"></p>
</form>
{% endblock %}
password_reset_done.html
パスワードリセットメール送信完了ページ
{% extends "blogapp/base.html" %}
{% load static %}
{% block content %}
<h1>Email transmission comlete</h1>
<p>Check your email</p>
{% endblock %}
password_reset_confirm.html
新しいパスワード設定ページ
{% extends "blogapp/base.html" %}
{% load static %}
{% block content %}
{% if validlink %}
<h1>Set a new Password</h1>
<form method="POST">
{% csrf_token %}
{{ form.as_p }}
<p><input type="submit" value="Change password"></p>
</form>
{% else %}
<p>The password reset link is invalid. It may have already been used.</P>
{% endif %}
{% endblock %}
password_reset_complete.html
新しいパスワード設定完了ページ
{% extends "blogapp/base.html" %}
{% load static %}
{% block content %}
<h1>Change password complete</h1>
{% endblock %}
password_reset_email.html
パスワード変更用URL付きメールのカスタマイズ
Reverse for 'password_reset_confirm' with keyword arguments '{'uidb64': '', 'token': ''}' not found. 1 pattern(s) tried: ['reset/(?P[^/]+)/(?P[^/]+)/\Z']
このエラーが最初何か理解できなかった💦
このテンプレートの内容はDjangoのエラーページのをコピペしてpassword_reset_confirm部分を変更しています。
(url app_name:password_reset_confirm)
{% load i18n %}{% autoescape off %}
{% blocktranslate %}You're receiving this email because you requested a password reset for your user account at {{ site_name }}.{% endblocktranslate %}
{% translate "Please go to the following page and choose a new password:" %}
{% block reset_link %}
{{ protocol }}://{{ domain }}{% url 'blogapp:password_reset_confirm' uidb64=uid token=token %}
{% endblock %}
{% translate 'Your username, in case you’ve forgotten:' %} {{ user.get_username }}
{% translate "Thanks for using our site!" %}
{% blocktranslate %}The {{ site_name }} team{% endblocktranslate %}
{% endautoescape %}
Runserverで確認
パスワードリセットページにアクセス
Send Mailすると
メールのデバッグはsettings.pyに下記を追加
EMAIL_BACKEND = "django.core.mail.backends.console.EmailBackend"
送信完了ページ
メールのURLにアクセスすると
新しいパスワードを設定して完了すると
まとめ
Djangoは楽しいですね。まだまだ全然わからないことのが多いですが💦
もっと色々できたら楽しそうです。
あと、このままではダサいのでCSSを作成して見栄え良くしていこうと思います。
参考サイト