How to reset Django admin password?

前端 未结 21 1829
遥遥无期
遥遥无期 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:39

    One of the best ways to retrieve the username and password is to view and update them. The User Model provides a perfect way to do so.

    In this case, I'm using Django 1.9

    1. Navigate to your root directory i,e. where you "manage.py" file is located using your console or other application such as Git.

    2. Retrieve the Python shell using the command "python manage.py shell".

    3. Import the User Model by typing the following command "from django.contrib.auth.models import User"

    4. Get all the users by typing the following command "users = User.objects.all()"

    5. Print a list of the users For Python 2 users use the command "print users" For Python 3 users use the command "print(users)" The first user is usually the admin.

    6. Select the user you wish to change their password e.g.

      "user = users[0]"

    7. Set the password

      user.set_password('name_of_the_new_password_for_user_selected')

    8. Save the new password

      "user.save()"

    Start the server and log in using the username and the updated password.

提交回复
热议问题