user_change_password() 得到了意外的关键字参数 'extra_context'

user_change_password() got an unexpected keyword argument 'extra_context'

Django 3.0.7

当我尝试在管理站点中更改密码时,我得到

TypeError at /admin/auth/user/1/password/
user_change_password() got an unexpected keyword argument 'extra_context'

即我按了“这个表格”link:

更多详情

Environment:


Request Method: GET
Request URL: http://localhost:8000/admin/auth/user/1/password/

Django Version: 3.0.7
Python Version: 3.8.0
Installed Applications:
['admin_aux',
 'images.apps.ImagesConfig',
 'django.contrib.admin',
 'django.contrib.auth',
 'django.contrib.contenttypes',
 'django.contrib.sessions',
 'django.contrib.messages',
 'django.contrib.staticfiles',
 'posts',
 'sidebars',
 'general',
 'categories',
 'marketing',
 'home',
 'authors',
 'taggit',
 'cachalot',
 'django_cleanup.apps.CleanupConfig',
 'widgets',
 'code_samples',
 'hyper_links',
 'polls',
 'applications',
 'videos',
 'quotations',
 'languages',
 'people',
 'arbitrary_htmls.apps.ArbitraryHtmlsConfig']
Installed Middleware:
['django.middleware.security.SecurityMiddleware',
 'django.contrib.sessions.middleware.SessionMiddleware',
 'django.middleware.common.CommonMiddleware',
 'django.middleware.csrf.CsrfViewMiddleware',
 'django.contrib.auth.middleware.AuthenticationMiddleware',
 'django.contrib.messages.middleware.MessageMiddleware',
 'django.middleware.clickjacking.XFrameOptionsMiddleware']



Traceback (most recent call last):
  File "/home/michael/PycharmProjects/pcask/venv/lib/python3.8/site-packages/django/core/handlers/exception.py", line 34, in inner
    response = get_response(request)
  File "/home/michael/PycharmProjects/pcask/venv/lib/python3.8/site-packages/django/core/handlers/base.py", line 115, in _get_response
    response = self.process_exception_by_middleware(e, request)
  File "/home/michael/PycharmProjects/pcask/venv/lib/python3.8/site-packages/django/core/handlers/base.py", line 113, in _get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
  File "/home/michael/PycharmProjects/pcask/venv/lib/python3.8/site-packages/django/utils/decorators.py", line 130, in _wrapped_view
    response = view_func(request, *args, **kwargs)
  File "/home/michael/PycharmProjects/pcask/venv/lib/python3.8/site-packages/django/views/decorators/cache.py", line 44, in _wrapped_view_func
    response = view_func(request, *args, **kwargs)
  File "/home/michael/PycharmProjects/pcask/venv/lib/python3.8/site-packages/django/contrib/admin/sites.py", line 231, in inner
    return view(request, *args, **kwargs)
  File "/home/michael/PycharmProjects/pcask/venv/lib/python3.8/site-packages/django/utils/decorators.py", line 43, in _wrapper
    return bound_method(*args, **kwargs)
  File "/home/michael/PycharmProjects/pcask/venv/lib/python3.8/site-packages/django/views/decorators/debug.py", line 76, in sensitive_post_parameters_wrapper
    return view(request, *args, **kwargs)

Exception Type: TypeError at /admin/auth/user/1/password/
Exception Value: user_change_password() got an unexpected keyword argument 'extra_context'

如何定位这个问题?

发生了什么...

我不知道。值得注意的是,url 应该在 django/contrib/auth/admin 的 User Auth ModelAdmin 中调用一个名为 user_change_password 的方法。它具有以下签名:

def user_change_password(self, request, id, form_url=""):

这就是引发错误的原因,因为不知何故 extra_context 被传递给它。

还有一种方法可以让您更改登录用户的密码,它接受 extra_context kwarg。我最好的猜测是其中一个应用程序覆盖了标准身份验证 ModelAdmin 并且做得不太正确。当然,新的 django 3.0.7 项目一切正常。

How can I localize this problem?

我会删除您所有的附加应用程序。希望这能解决问题。如果没有,那么这将变得更加有趣。但如果确实如此,我会把它们一个一个地添加回来,直到它坏掉,然后你就会找出是哪个额外的应用程序坏了东西。

maybe you even can help me cope with it.

如果您只想更改密码,我可以想到一些您可以做的事情。您可以通过 shell:

更改用户详细信息
python manage.py shell

然后,您可以通过以下方式更改密码:

from auth.models import User
user = User.objects.get(id=1)  # Or whatever user you want
user.set_password('my_new_password')
user.save()

这应该可以解决问题。更简单的是,有一个管理命令可以执行此操作(但为此您需要知道当前密码)。您可以简单地 运行:

manage.py changepassword *username* 

我有这个问题(虽然在 Django 2.2 中)。它是在我听取了 adding extra_context to every admin page. This seems harmless, but breaks the "change password" form. In the end I deleted the 'extra_context' changes to urls.py and added the extra_context ONLY to the admin forms I needed using the ModelAdmin add_view and change_view methods.

上另一个 Stack Overflow post 的建议后开始的
# New Layer Form
def add_view(self, request, form_url='', extra_context={}):
    extra_context['CATALOG_TECHNOLOGY'] = settings.CATALOG_TECHNOLOGY
    return super(LayerAdmin, self).add_view(request, form_url, extra_context)

# Edit Layer Form
def change_view(self, request, object_id, extra_context={}):
    extra_context['CATALOG_TECHNOLOGY'] = settings.CATALOG_TECHNOLOGY
    return super(LayerAdmin, self).change_view(request, object_id, extra_context=extra_context)

以下是完整上下文的提交: