Lucas Cyrne
Lucas Cyrne

Reputation: 51

Django NameError: name 'password_reset_done' is not defined

found some topics like this but they are older or not solved.

Well, I'm following a youtube tutorial and I'm stuck in this part of it, I already know it's due the difference of version, I've gone to the docs and got some answers but still can't solve it by myself.

I'll post what I think it's relevant but if you guys want another part of my code ask and I'll get it.

the error as I said (on Traceback) is: NameError: name 'password_reset_done' is not defined

On the tutorial he didn't done anything on views.py about this, he just added some imports on url.py that was deprecated so I fix that but the errors keeps the same...

My accounts/urls.py:

from django.urls import path
from . import views
from django.contrib.auth.views import (
    login,
    logout,
    password_reset,
    PasswordResetDoneView,
    PasswordResetConfirmView,
)


urlpatterns = [

    path('', views.home),
    path('login/', login, {'template_name': 'contas/login.html'}),
    path('logout/', logout, {'template_name': 'contas/logout.html'}),
    path('register/', views.register, name='register'),
    path('perfil/', views.view_perfil, name='view_perfil'),
    path('perfil/edit/', views.edit_perfil, name='edit_perfil'),
    path('trocar-password/', views.trocar_password, name='trocar_password'),
    path('reset-password/', password_reset, name='reset_password'),
    path('reset-password/done/', password_reset_done, name='password_reset_done'),
    path('reset-password/confirm/(?P<uidb64>[0-9A-Za-z]+)-(?P<token>.+)/',
    password_reset_confirm, name='password_reset_confirm'),

]

just to add some more code. My views.py:

from django.shortcuts import render, redirect, HttpResponse
from contas.forms import (
RegistrationForm,
EditPerfilForm,
)
from django.contrib.auth.models import User
from django.contrib.auth.forms import UserChangeForm, PasswordChangeForm
from django.contrib.auth import update_session_auth_hash


def home(request):
    numbers = [1,2,3,4,5]
    name = 'Lucas Cyrne'

    args = {'myName': name, 'numbers': numbers}
    return render(request, 'contas/home.html', args)

def register(request):
  if request.method=='POST':
    form = RegistrationForm(request.POST)
    if form.is_valid():
        form.save()
        return redirect('/contas')
  else:
    form = RegistrationForm()

  args = {'form':form}
  return render(request, 'contas/reg_form.html', args)

def view_perfil(request):
    args = {'user': request.user}
    return render(request, 'contas/perfil.html', args)

def edit_perfil(request):
 if request.method=='POST':
    form = EditPerfilForm(request.POST, instance=request.user)

    if form.is_valid():
        form.save()
        return redirect('/contas/perfil')
 else:
    form = EditPerfilForm(instance=request.user)
    args = {'form': form}
    return render(request, 'contas/edit_perfil.html', args)

def trocar_password(request):
  if request.method=='POST':
    form = PasswordChangeForm(data=request.POST, user=request.user)

    if form.is_valid():
        form.save()
        update_session_auth_hash(request, form.user)
        return redirect('/contas/perfil')
    else:
        return redirect('/contas/trocar_password')
  else:
    form = PasswordChangeForm(user=request.user)

    args = {'form': form}
    return render(request, 'contas/trocar_password.html', args)

Upvotes: 0

Views: 1476

Answers (3)

Lemayzeur
Lemayzeur

Reputation: 8525

Django looks for everywhere, and doesn't see password_reset_done

and above your urls patter, there is this:

NOTE: these built-in CBV are available forn Django >= 1.11

from django.contrib.auth.views import (
login,
logout,
password_reset, # suggestion: PasswordResetView
PasswordResetDoneView,
PasswordResetConfirmView,
)

you view is PasswordResetDoneView

instead of:

path('reset-password/done/', password_reset_done, name='password_reset_done'), 

it should be:

path('reset-password/done/', PasswordResetDoneView.as_view(), name='password_reset_done'), 

Upvotes: 2

BugHunter
BugHunter

Reputation: 1204

  1. You need to add app_name is urls.py
  2. Add views as viewname.as_view() in urls.py

Upvotes: 0

seuling
seuling

Reputation: 2966

Cause you didn't call password_reset_done view, but using it in urls.

path('reset-password/done/', password_reset_done, name='password_reset_done'),

Look this url, you are trying to use view that doesn't exist.

Make your own view inheriting PasswordResetDoneView or, just use it without registering url. (You only can call url using password_reset_done like this

reverse('password_reset_done')

p.s. you have to add django.contrib.auth.urls to your urls.py. like

url('', include('django.contrib.auth.urls')), ( for django =< 1.11 )

Upvotes: 0

Related Questions