Michael
Michael

Reputation: 4461

user_change_password() got an unexpected keyword argument 'extra_context'

Django 3.0.7

When I try to change password in admin site, I get

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

Namely I pressed "this form" link:

enter image description here

More details

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'

How can I localize this problem?

Upvotes: 0

Views: 765

Answers (2)

Ryan
Ryan

Reputation: 483

I had this issue (though in Django 2.2). It started after I took the advice from another Stack Overflow post on 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.

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

Here are the commits for full context:

Upvotes: 1

tim-mccurrach
tim-mccurrach

Reputation: 6835

What's happening...

I have no idea tbh. It is worth noting a few things though, that the url, should call a method in the User Auth ModelAdmin at django/contrib/auth/admin called user_change_password. It has the following signature:

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

which is why the error is being raised, because somehow extra_context is being passed to it.

There is also a way for you to change the logged on users password, which does accept a extra_context kwarg. My best guess is that one of the apps has overwritten the standard auth ModelAdmin and done it not quite right. Certainly everything works fine with a fresh django 3.0.7 project.

How can I localize this problem?

I would remove all of your additional apps. Hopefully this will fix the problem. If it doesn't then this becomes more interesting. But if it does, I would add them back in one by one until it breaks, and then you'll figure out which additional app is breaking things.

maybe you even can help me cope with it.

There's a few things I can think of that you could do if you just want to change the password. You can change user details via the shell:

python manage.py shell

Then the following will enable you to change a password:

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

This should do the trick. Even easier still, there is a management command that does it (but for this you will need to know the current password). You can simply run:

manage.py changepassword *username* 

Upvotes: 1

Related Questions