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
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.
If you forgot create admin user first build one with createsuperuser
command on manage.py
then change the password.
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.
You can create a new superuser with createsuperuser command.
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 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;