简体   繁体   中英

Django 2.1 PasswordResetView 404

I'm trying to integrate the User password reset functionality into my custom user model. When I try to access the URL I get a 404-error.

The URL I try to access: http://localhost:8000/reset/MQ/4zw-f78ac4c97d45f366a243/

My 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'),
]

My 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)

The function of sending the reset link is working. I get the email inside my console and the link I shown before is printing.

In my template file i already had to make an ugly hack....

{% 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 %}

I had to hardcore the URL since

{{ protocol }}://{{ domain }}{% url 'accounts:password-reset-confirm' uidb64=uid token=token %}

simply wouldn't work and also gave me an error. Maybe someone could explain to me what I'm doing wrong. Trying to figure this out for some time now.

You have

path('reset/<uuid:uidb64>/<slug:token>/',
     views.PasswordResetConfirmView.as_view(),
     name='password_reset_confirm'),

However uidb64 is not a uuid, it is a string like MQ .

To fix the problem, change the URL pattern to match the entry in django.contrib.auth.urls :

path('reset/<uidb64>/<token>/', views.PasswordResetConfirmView.as_view(), name='password_reset_confirm'),

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM