How to reset Django admin password?

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

    In case you do not know the usernames as created here. You can get the users as described by @FallenAngel above.

    python manage.py shell 
    from django.contrib.auth.models import User
    usrs = User.objects.filter(is_superuser=True)
    #identify the user
    your_user = usrs.filter(username="yourusername")[0]
    #youruser = usrs.get(username="yourusername")
    #then set the password
    

    However in the event that you created your independent user model. A simple case is when you want to use email as a username instead of the default user name. In which case your user model lives somewhere such as your_accounts_app.models then the above solution wont work. In this case you can instead use the get_user_model method

    from django.contrib.auth import get_user_model 
    super_users = get_user_model().objects.filter(is_superuser=True)
    #proceed to get identify your user
    # and set their user password
    
    0 讨论(0)
  • 2020-12-12 09:36

    python manage.py changepassword username

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

    Another way to get the user name (and most of the information) is to access the database directly and read the info from the tables.

    0 讨论(0)
  • 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.

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

    Another thing that is worth noting is to set your user's status is_staff as active. At least, that's what makes it works for me. For more detail, I created another superuser as people explained above. Then I go to the database table auth_user and search for that username to make sure its is_staff flag is set to 1. That finally allowed me to log into admin site.

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

    use

    python manage.py dumpdata

    then look at the end you will find the user name

    0 讨论(0)
提交回复
热议问题