パスワードリセットのURLとテンプレートの位置を変更したくて、PasswordResetViewを継承したクラスを作ったけどsuccess_url設定しなかったらNoReverseMatchでドツボにハマった件。
CustomPasswordResetViewクラス作ってテストすると
class CustomPasswordResetView(PasswordResetView):
template_name = 'password_reset.html'
email_template_name = 'password_reset_email.html'
NoReverseMatch発生。
NoReverseMatch at /account/password_reset/
Reverse for 'password_reset_confirm' not found. 'password_reset_confirm' is not a valid view function or pattern name.
Request Method: POST
Request URL: http://127.0.0.1:8000/account/password_reset/
Django Version: 5.1.6
Exception Type: NoReverseMatch
Exception Value:
Reverse for 'password_reset_confirm' not found. 'password_reset_confirm' is not a valid view function or pattern name.
Exception Location: /workspace/senrigan/.venv/lib/python3.12/site-packages/django/urls/resolvers.py, line 831, in _reverse_with_prefix
Raised during: django.contrib.auth.views.PasswordResetView
pathの設定は特に間違ってないと思うけど。
path('password_reset/', CustomPasswordResetView.as_view(), name='password_reset'),
path('password_reset_done/', CustomPasswordResetDoneView.as_view(), name='password_reset_done'),
path('password_reset_confirm/<uidb64>/<token>/', auth_views.PasswordResetConfirmView.as_view(), name='password_reset_confirm'),
いつものようにCopilitに聞いたら、success_urlを設定しろというので言うとおりにしたらなおった。
success_url = reverse_lazy('account:password_reset_done')
あー、PasswordResetViewクラスに修正加えてるから、urlの評価は実際にビューがレンダリングされるときまで遅延させないといけないのかと思ったけど、PasswordResetViewクラスに定義してあるね。
success_url = reverse_lazy("password_reset_done")
viewnameが違うね。。。問題はviewnameが違うことだったか。試しにReverse()使ったらどうなるのかと思ってやってみたけど、InproperlyConfiguredになった。なんで遅延評価の部分も問題なのかな。結局、class-based viewではreverse_lazy()で最後の最後にURLの生成が必要になったときに評価されないとダメみたい。
raise ImproperlyConfigured(msg.format(name=self.urlconf_name)) from e
django.core.exceptions.
ImproperlyConfigured: The included URLconf 'app.urls' does not appear to have any patterns in it.
If you see the 'urlpatterns' variable with valid patterns in the file then the issue is probably caused by a circular import.