Django __init__() 得到了一个意外的关键字参数“用户”

Posted

技术标签:

【中文标题】Django __init__() 得到了一个意外的关键字参数“用户”【英文标题】:Django __init__() got an unexpected keyword argument 'user' 【发布时间】:2021-11-17 23:11:41 【问题描述】:

我正在尝试使用默认的 django 库构建密码重置系统。问题是在尝试进入要更改它的站点(PasswordChangeConfirm)后出现此错误跟踪。

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'

我的代码: 视图.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>

我正在使用小部件调整来呈现表单,但我想这不是问题,因为对于其他事情,它可以工作。

【问题讨论】:

【参考方案1】:

你的password_reset_view继承自PasswordResetConfirmView [Django docs],这个视图通常使用SetPasswordForm的形式,它以用户为参数(因为它需要设置用户密码),问题是你的形式CaptchaPasswordResetForm继承自PasswordResetForm(不正确),因此您会收到错误消息。您应该更新您的表单类以正确继承:

from django.contrib.auth.forms import SetPasswordForm


class CaptchaPasswordResetForm(SetPasswordForm):
    ...

【讨论】:

好吧,这行得通,但现在我有另一个错误:hastebin.com/ijokirojuy.sql

以上是关于Django __init__() 得到了一个意外的关键字参数“用户”的主要内容,如果未能解决你的问题,请参考以下文章

django-filter:/goods/ __init__() 的 TypeError 得到了一个意外的关键字参数“名称”

Django CharField TypeError: __init__() 得到了一个意外的关键字参数“empty_value”

TypeError: __init__() 在 Django Form & Formset 中有一个意外的关键字参数

python 2.7 上的错误“__init__() 得到了意外的关键字参数‘tcp_nodelay’”

Django Channels __init__() 有一个意外的关键字参数“范围”

TypeError: __init__() 得到了一个意外的关键字参数 '__no_builder' Kivy