'Django __init__() got an unexpected keyword argument 'user'
Im trying to build a password reset system using the default django libraries. The problem is after trying to enter the site which is for changing it (PasswordChangeConfirm) this error trace appears.
Internal Server Error: /reset/confirm/MQ/atg5tq-aa6bcab1c34d1228cdd1970aaaa45252/
Traceback (most recent call last):
File "C:\Users\Finn\AppData\Local\Programs\Python\Python39\lib\site-packages\django\core\handlers\exception.py", line 47, in inner
response = get_response(request)
File "C:\Users\Finn\AppData\Local\Programs\Python\Python39\lib\site-packages\django\core\handlers\base.py", line 181, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "C:\Users\Finn\AppData\Local\Programs\Python\Python39\lib\site-packages\django\views\generic\base.py", line 70, in view
return self.dispatch(request, *args, **kwargs)
File "C:\Users\Finn\AppData\Local\Programs\Python\Python39\lib\site-packages\django\utils\decorators.py", line 43, in _wrapper
return bound_method(*args, **kwargs)
File "C:\Users\Finn\AppData\Local\Programs\Python\Python39\lib\site-packages\django\views\decorators\debug.py", line 89, in sensitive_post_parameters_wrapper
return view(request, *args, **kwargs)
File "C:\Users\Finn\AppData\Local\Programs\Python\Python39\lib\site-packages\django\utils\decorators.py", line 43, in _wrapper
return bound_method(*args, **kwargs)
File "C:\Users\Finn\AppData\Local\Programs\Python\Python39\lib\site-packages\django\views\decorators\cache.py", line 44, in _wrapped_view_func
response = view_func(request, *args, **kwargs)
File "C:\Users\Finn\AppData\Local\Programs\Python\Python39\lib\site-packages\django\contrib\auth\views.py", line 284, in dispatch
return self.render_to_response(self.get_context_data())
File "C:\Users\Finn\AppData\Local\Programs\Python\Python39\lib\site-packages\django\contrib\auth\views.py", line 308, in get_context_data
context = super().get_context_data(**kwargs)
File "C:\Users\Finn\AppData\Local\Programs\Python\Python39\lib\site-packages\django\contrib\auth\views.py", line 200, in get_context_data
context = super().get_context_data(**kwargs)
File "C:\Users\Finn\AppData\Local\Programs\Python\Python39\lib\site-packages\django\views\generic\edit.py", line 66, in get_context_data
kwargs['form'] = self.get_form()
File "C:\Users\Finn\AppData\Local\Programs\Python\Python39\lib\site-packages\django\views\generic\edit.py", line 33, in get_form
return form_class(**self.get_form_kwargs())
TypeError: __init__() got an unexpected keyword argument 'user'
my code: views.py
class password_reset_view(PasswordResetConfirmView):
form_class = CaptchaPasswordResetForm
success_url = reverse_lazy('home:login')
urls.py
urls.py
from StartSite.views import password_reset_view
from StartSite.forms import NewPasswordResetForm
from StartSite.forms import CaptchaPasswordResetForm
from django.contrib import admin
from django.urls import path, include
from django.urls.base import reverse_lazy
from django.contrib.auth import views as auth_views
from StartSite.views import PasswordResetView, password_reset_email_view
urlpatterns = [
path('admin/', admin.site.urls),
path('', include('StartSite.urls')),
path('reset_password/',
password_reset_email_view.as_view(template_name="home/password_reset.html", form_class=NewPasswordResetForm),
name="reset_password"),
path('reset_password_sent/',
auth_views.PasswordResetDoneView.as_view(template_name="home/password_reset_sent.html"),
name="password_reset_done"),
path('reset/confirm/<uidb64>/<token>/',
password_reset_view.as_view(template_name="home/password_reset_form.html", form_class=CaptchaPasswordResetForm),
name="password_reset_confirm"),
path('reset_password_complete/',
auth_views.PasswordResetCompleteView.as_view(template_name="home/password_reset_done.html"),
name="password_reset_complete"),
]
forms.py
class CaptchaPasswordResetForm(PasswordResetForm):
new_password1 = forms.forms.CharField(max_length=100, widget=forms.forms.PasswordInput(attrs={"class": "form-control", 'type':'password'}))
new_password2 = forms.forms.CharField(max_length=100, widget=forms.forms.PasswordInput(attrs={"class": "form-control", 'type':'password'}))
html
<form method="post" class="id_password_reset_form">
{% csrf_token %}
<p>Please enter your new password</p>
<h6 class="mb-0">New Password</h6>
<div class="pw_space2"></div>
{% render_field form.new_password1 class="form-control" type="password" placeholder="New Password" data-form-field="password"%}
<div class="pw_space"></div>
<h6 class="mb-0">Repeat New Password</h6>
<div class="pw_space2"></div>
{% render_field form.new_password2 class="form-control" type="password" placeholder="Repeat New Password" data-form-field="password"%}
{{form.errors}}
<div class="pw_space2"></div>
<button style="background-color:#7231aa; border:#7231aa; color:white;" class="btn btn-secondary " type="submit">Change Password</button>
<a style="background-color:#7231aa; border:#7231aa; color:white;" class="btn btn-secondary " target="__blank" href="{% url 'home:profilesettings' %}">Cancel</a>
</form>
Im using widget tweaks to render the form but I guess thats not the problem because for other things this works.
Solution 1:[1]
Your password_reset_view
inherits from PasswordResetConfirmView
[Django docs], this view normally uses the form SetPasswordForm
which takes the user as an argument (because it needs to set the users password), the problem is that your form CaptchaPasswordResetForm
instead inherits from PasswordResetForm
(incorrect) and hence you get an error. You should update your form class to inherit correctly:
from django.contrib.auth.forms import SetPasswordForm
class CaptchaPasswordResetForm(SetPasswordForm):
...
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Abdul Aziz Barkat |