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?
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?
In addition to @JamesO answer that states using
python manage.py changepassword [username]
1- while in your project's main directory access the database (I'm using sqlite3):
sqlite3 db.sqlite3
2- list the content of the auth_user table
SELECT * FROM auth_user ;
3- look for the user that has is_superuser = 1 , in my case it's admin
screenshot of the command output (I don't have enough rep points)
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'
You may try this:
1.Change Superuser password without console
python manage.py changepassword <username>
2.Change Superuser password through console
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()
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
(venv)your_prj $ ./manage.py shell
>>> from customusers.models import CustomUser
>>> CustomUser.objects.filter(is_superuser=True)
>>> user = CustomUser.objects.get(email="somesuper@sys.com")
>>> user.set_password('@NewPwd')
>>> user.save()
>>> exit()
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.
Just type this command in your command line:
python manage.py changepassword yourusername
If a reset_password link is needed in the /admin/ website this is the way to go:
https://docs.djangoproject.com/en/3.2/ref/contrib/admin/#adding-a-password-reset-feature
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.