Django忘记密码邮件系统出现NoReverseMatch错误求助
Hey there! I get how frustrating it is when your password reset system works functionally but throws those annoying NoReverseMatch errors—let’s nail this down. Since the core features (sending emails, resetting passwords) are working, the issue almost always boils down to mismatched URL names or missing namespace references in your templates/views.
Common Causes & Fixes
1. Misconfigured URL Names in urls.py
First, double-check that all your password reset URLs have the correct name attributes, and they match what you’re referencing elsewhere. Here’s the standard setup to compare against:
from django.contrib.auth import views as auth_views urlpatterns = [ # ... your other URLs path('password-reset/', auth_views.PasswordResetView.as_view(), name='password_reset'), path('password-reset/done/', auth_views.PasswordResetDoneView.as_view(), name='password_reset_done'), path('reset/<uidb64>/<token>/', auth_views.PasswordResetConfirmView.as_view(), name='password_reset_confirm'), path('reset/done/', auth_views.PasswordResetCompleteView.as_view(), name='password_reset_complete'), ]
If you’re using an app_name in your app’s urls.py (like app_name = 'accounts'), every time you reference these URLs, you need to include the namespace prefix (e.g., accounts:password_reset instead of just password_reset).
2. Incorrect Link in Password Reset Email Template
The most common culprit is the reset link in your email template (password_reset_email.html). Make sure the URL name here exactly matches the one in urls.py. For example:
Click this link to reset your password: <a href="{{ protocol }}://{{ domain }}{% url 'password_reset_confirm' uidb64=uid token=token %}">Reset Password</a>
If you have a namespace, update it to:
{% url 'accounts:password_reset_confirm' uidb64=uid token=token %}
A tiny typo here (like password_reset_confirmed instead of password_reset_confirm) will trigger the error even though the link still works (since the URL path is correct, but the reverse lookup fails when rendering the email or redirecting).
3. Mismatched success_url in Views
If you’ve customized any of the password reset views (like PasswordResetView), ensure the success_url uses the correct reversed URL. For example:
from django.contrib.auth.views import PasswordResetView from django.urls import reverse_lazy class CustomPasswordResetView(PasswordResetView): success_url = reverse_lazy('password_reset_done') # Must match the name in urls.py # ... other customizations
The default views use the correct names out of the box, but if you’ve overridden this, a wrong name here will cause a redirect error after submitting the email form.
4. Wrong Form Action in Password Reset Template
Check the action attribute of your password reset form (in password_reset.html). It should point to the correct URL name:
<form method="post" action="{% url 'password_reset' %}"> {% csrf_token %} {{ form.as_p }} <button type="submit">Send Reset Link</button> </form>
Again, add the namespace prefix if you’re using one.
Quick Troubleshooting Steps
- Run
python manage.py show_urls(if you have django-extensions installed) to list all registered URLs and their names—this helps you spot mismatches instantly. - Look at the full error traceback: it will tell you exactly which URL name Django is trying to reverse and failing on. That’s your clue to where the mismatch is.
Since your core functionality works, the fix is almost certainly a small typo or missing namespace reference somewhere in these areas.
内容的提问来源于stack exchange,提问作者cbuch1800

