我正在尝试将用户密码重置功能集成到我的自定义用户模型中。当我尝试访问URL时,出现404错误。
我尝试访问的URL: http://localhost:8000/reset/MQ/4zw-f78ac4c97d45f366a243/
我的urls.py:
from django.urls import path
from django.urls import reverse_lazy
from django.contrib.auth.views import PasswordResetView
from . import views
app_name = 'accounts'
urlpatterns = [
path('signup/', views.SignUpView.as_view(), name='signup'),
path('login/', views.LoginView.as_view(), name='login'),
path('logout/', views.LogOutView.as_view(), name='logout'),
path('password-change/', views.PasswordChangeView.as_view(),
name='password-change'),
path('password-reset/', views.PasswordResetView.as_view(),
name='password-reset'),
path('password-reset-done/', views.PasswordResetDoneView.as_view(),
name='password-reset-done'),
path('reset/<uuid:uidb64>/<slug:token>/',
views.PasswordResetConfirmView.as_view(),
name='password_reset_confirm'),
]
我的views.py:
from django.shortcuts import render
from braces import views as bracesviews
from django.views import generic
from .forms import RegisterForm, LoginForm, PasswordResetForm, SetPasswordForm, PasswordChangeForm
from django.contrib.auth import get_user_model
from django.contrib.auth import views as auth_views
from django.contrib.auth import authenticate, login, logout
from django.utils.translation import ugettext, ugettext_lazy as _
from django.urls import reverse_lazy
from django.contrib import auth
# Create your views here.
User = get_user_model()
(...)
class PasswordChangeView(auth_views.PasswordChangeView):
form_class = PasswordChangeForm
template_name = 'accounts/password-change.html'
success_url = reverse_lazy('home')
form_valid_message = _("Your password was changed!")
def form_valid(self, form):
form.save()
return super().form_valid(form)
class PasswordResetView(auth_views.PasswordResetView):
form_class = PasswordResetForm
template_name = 'accounts/password-reset.html'
success_url = reverse_lazy('accounts:password-reset-done')
subject_template_name = 'accounts/emails/password-reset-subject.txt'
email_template_name = 'accounts/emails/password-reset-email.html'
class PasswordResetDoneView(auth_views.PasswordResetDoneView):
template_name = 'accounts/password-reset-done.html'
class PasswordResetConfirmView(auth_views.PasswordResetConfirmView):
template_name = 'accounts/password-reset-confirm.html'
form_class = SetPasswordForm
success_url = reverse_lazy('home')
form_valid_message = _("Your password was changed!")
def form_valid(self, form):
form.save()
return super().form_valid(form)
发送重置链接的功能正在工作。我收到控制台中的电子邮件,并且之前显示的链接正在打印。
在我的模板文件中,我已经不得不进行难看的修改了。...
{% load i18n %}{% autoescape off %}
{% blocktrans %}You're receiving this email because you requested a password reset for your user account at
{{ site_name }}.{% endblocktrans %}
{% trans "Please go to the following page and choose a new password:" %}
{% block reset_link %}
{{ protocol }}://{{ domain }}/reset/{{ uid }}/{{ token }}/
{% endblock %}
{% trans "Your login email, in case you've forgotten, is same this email address:" %} {{ user.get_username }}
{% trans "Thanks for using our site!" %}
{% blocktrans %}The {{ site_name }} team{% endblocktrans %}
{% endautoescape %}
自
以来,我必须对URL进行加重标记{{ protocol }}://{{ domain }}{% url 'accounts:password-reset-confirm' uidb64=uid token=token %}
简直行不通,还给我一个错误。也许有人可以向我解释我做错了什么。现在尝试解决这个问题。
答案 0 :(得分:2)
您有
path('reset/<uuid:uidb64>/<slug:token>/',
views.PasswordResetConfirmView.as_view(),
name='password_reset_confirm'),
但是uidb64
不是uuid,而是类似MQ
的字符串。
要解决此问题,请更改URL模式以匹配django.contrib.auth.urls
中的条目:
path('reset/<uidb64>/<token>/', views.PasswordResetConfirmView.as_view(), name='password_reset_confirm'),