How to reset Django admin password?

前端 未结 21 1706
遥遥无期
遥遥无期 2020-12-12 08:39

I am using Django (version 1.3) and have forgotten both admin username and password. How to reset both?

And is it possible to make a normal user into admin, and then

相关标签:
21条回答
  • 2020-12-12 09:30

    new setup should first run python manage.py createsuperuser to create user. It seems like there is no default username password to login into admin.

    0 讨论(0)
  • 2020-12-12 09:30

    If you forgot create admin user first build one with createsuperuser command on manage.py then change the password.

    0 讨论(0)
  • 2020-12-12 09:30

    Create a new superuser with the command "python manage.py createsuperuser". Login as the new super user. Click on the 'users' link. Then click on the user you want to delete. click on delete user at the end of the form page.

    Note - The above process will make changes to the activity logs done by that particular user.

    0 讨论(0)
  • 2020-12-12 09:32

    You can create a new superuser with createsuperuser command.

    0 讨论(0)
  • 2020-12-12 09:34

    Two ways to do this:

    The changepassword management command:

    (env) $ python manage.py changepassword <username>
    

    Or (which expands upon a few answers, but works for any extended User model) using the django-admin shell as follows:

    (env) $ python manage.py shell
    

    This should bring up the shell command prompt as follows:

    Python 3.7.2 (default, Mar 27 2019, 08:44:46) 
    [GCC 6.3.0 20170516] on linux
    Type "help", "copyright", "credits" or "license" for more information.
    (InteractiveConsole)
    >>>
    

    Then you would want the following:

    >>> from django.contrib.auth import get_user_model
    >>> User = get_user_model()
    >>> user = User.objects.get(username='admin@hug.com')
    >>> user.set_password('new password')
    >>> user.save()
    >>> exit()
    

    N.B. Why have I answered this question with this answer?

    Because, as mentioned, User = get_user_model() will work for your own custom User models. Using from django.contrib.auth.models import User then User.objects.get(username='username') may throw the following error:

    AttributeError: Manager isn't available; 'auth.User' has been swapped for 'users.User'
    
    0 讨论(0)
  • 2020-12-12 09:35

    You may also have answered a setup question wrong and have zero staff members. In which case head to postgres:

    obvioustest=# \c [yourdatabasename]
    obvioustest=# \x
    obvioustest=# select * from auth_user;
    -[ RECORD 1 ]+-------------
    id           | 1
    is_superuser | f
    is_staff     | f
    ...
    

    To fix, edit directly:

    update auth_user set is_staff='true' where id=1;
    
    0 讨论(0)
提交回复
热议问题