518

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 remove admin status?

Machavity
  • 28,730
  • 25
  • 78
  • 91
IamH1kc
  • 5,442
  • 4
  • 15
  • 15

21 Answers21

908
python manage.py changepassword <user_name>

see docs

Rik Schoonbeek
  • 2,501
  • 1
  • 15
  • 30
JamesO
  • 21,360
  • 4
  • 37
  • 40
  • 7
    just realised that you said you forgot the username... 'admin' being the username in my answer wont be much help then! – JamesO Jun 15 '11 at 13:10
  • 8
    @user794916 That's rather strange choice if you don't remember the username. :-) – DrTyrsa Jun 15 '11 at 13:44
  • 1
    You can change the user name from "admin" to another proper user nam. :D – mxi1 Mar 04 '15 at 06:58
  • You can open the sqlite database with a third party app and check the usernames (not the passwords) – Vassilis Aug 22 '16 at 18:05
  • 4
    django's the best. everything is just so easy – DMTintner Mar 16 '17 at 16:27
  • 12
    If you've forgotten the username, you can do `manage.py createsuperuser` to create a 2nd superuser, and then view the list of Users to find the original one. Then log in as the original one and delete the new superuser. – shacker May 14 '18 at 16:09
  • 1
    If you just type command as shown in this answer without specifying a username, the command will let you change the password for the current system user and display it while doing so. – donyd Mar 16 '21 at 12:37
195
  1. python manage.py createsuperuser will create another superuser, you will be able to log into admin and rememder your username.
  2. Yes, why not.

To give a normal user privileges, open a shell with python manage.py shell and try:

from django.contrib.auth.models import User
user = User.objects.get(username='normaluser')
user.is_superuser = True
user.save()
Sursh
  • 25
  • 4
DrTyrsa
  • 27,759
  • 7
  • 79
  • 83
139

You may try through console:

python manage.py shell

then use following script in shell

from django.contrib.auth.models import User
User.objects.filter(is_superuser=True)

will list you all super users on the system. if you recognize yur username from the list:

usr = User.objects.get(username='your username')
usr.set_password('raw password')
usr.save()

and you set a new password (:

FallenAngel
  • 15,966
  • 10
  • 78
  • 105
  • 12
    `User.objects.filter(is_superuser=True)` – DrTyrsa Jun 15 '11 at 13:48
  • `pip install django-extensions`, add it to `installed apps` and run `./manage.py shell_plus`. The `User` class will be available and you can `password-change`. – Timo Mar 02 '18 at 13:45
  • This method allows user to set any password, without any validation. You can thus set a very simple password (just for test purposes), which the accepted answer method does not allow. – Gautam J Feb 01 '20 at 15:27
24

You can create a new superuser with createsuperuser command.

Gottlieb Notschnabel
  • 8,768
  • 17
  • 66
  • 107
Aldarund
  • 14,747
  • 4
  • 55
  • 86
16

This is very good question.

python manage.py changepassword user_name

Example :-

python manage.py changepassword mickey
anand24
  • 191
  • 1
  • 5
12

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.

Community
  • 1
  • 1
  • The advantage of this method over `manage.py changepassword` is that you're not restricted to what passwords you can or can't use - no more too similar to username crap. (This is in my dev environment. I'm not advocating weaker passwords. You should always use strong passwords in prod.) – Shashwat Black Mar 09 '18 at 02:59
11

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.

Kiran P.
  • 1,397
  • 18
  • 30
7

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'
jerrymouse
  • 14,255
  • 15
  • 61
  • 83
Micheal J. Roberts
  • 1,771
  • 12
  • 35
6

You may try this:

1.Change Superuser password without console

python manage.py changepassword <username>

2.Change Superuser password through console

enter image description here enter image description here

Mr Singh
  • 2,997
  • 2
  • 28
  • 42
  • 1
    Using screenshots instead of written code is very bad practice. Poeple can not Copy-Paste your code to test and it is not available for in-site-search – FallenAngel Mar 17 '20 at 07:19
5

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;
Gottlieb Notschnabel
  • 8,768
  • 17
  • 66
  • 107
Bryce
  • 7,335
  • 6
  • 51
  • 71
5

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

Martin Krung
  • 1,013
  • 7
  • 22
rapid2share
  • 524
  • 7
  • 6
3

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
unlockme
  • 2,650
  • 1
  • 21
  • 34
3

if you forget your admin then you need to create new user by using

python manage.py createsuperuser <username>

and for password there is CLI command changepassword for django to change user password

python manage.py changepassword <username>

OR

django-admin changepassword <username>

OR Run this code in Django env

from django.contrib.auth.models import User
u = User.objects.get(username='john')
u.set_password('new password')
u.save()
Saurabh Chandra Patel
  • 9,983
  • 3
  • 77
  • 72
2

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.

user1330974
  • 2,235
  • 4
  • 24
  • 47
2

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.

Ishan Dixit
  • 189
  • 1
  • 2
  • 10
1
python manage.py changepassword username
Samsul Islam
  • 2,322
  • 2
  • 13
  • 20
Basant Rules
  • 613
  • 7
  • 8
1

use

python manage.py dumpdata

then look at the end you will find the user name

1

Just type this command in your command line:

python manage.py changepassword yourusername
Tomerikoo
  • 12,112
  • 9
  • 27
  • 37
Shrey
  • 26
  • 3
0

The best way is to just go to your terminal and type

python manage.py createsuperuser

and insert another password and user name again but u will lost some of your profile that u have created before in most cases.

Dharman
  • 21,838
  • 18
  • 57
  • 107
CAP.tech
  • 47
  • 8
0

I think,The better way At the command line

python manage.py createsuperuser

-4

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.

Mutken
  • 125
  • 9