221π
python manage.py createsuperuser
will create another superuser, you will be able to log into admin and rememder your username.- 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()
- [Django]-How to format time in django-rest-framework's serializer?
- [Django]-Django model constraint for related objects
- [Django]-Django β How to set default value for DecimalField in django 1.3?
175π
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 (:
- [Django]-Serializer call is showing an TypeError: Object of type 'ListSerializer' is not JSON serializable?
- [Django]-Django: Using F arguments in datetime.timedelta inside a query
- [Django]-Form with CheckboxSelectMultiple doesn't validate
- [Django]-Sending an SMS to a Cellphone using Django
- [Django]-How do you detect a new instance of the model in Django's model.save()
- [Django]-How can I get MINIO access and secret key?
26π
In addition to @JamesO answer that states using
python manage.py changepassword [username]
if you donβt remember your 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)
- [Django]-Disabled field is not passed through β workaround needed
- [Django]-Django β "Incorrect type. Expected pk value, received str" error
- [Django]-Are sessions needed for python-social-auth
18π
This is very good question.
python manage.py changepassword user_name
Example :-
python manage.py changepassword mickey
- [Django]-Django models: default value for column
- [Django]-Django Rest Framework Conditional Field on Serializer
- [Django]-Django urlsafe base64 decoding with decryption
15π
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
-
Navigate to your root directory i,e. where you "manage.py" file is located using your console or other application such as Git.
-
Retrieve the Python shell using the command "python manage.py shell".
-
Import the User Model by typing the following command
"from django.contrib.auth.models import User" -
Get all the users by typing the following command
"users = User.objects.all()" -
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. -
Select the user you wish to change their password e.g.
"user = users[0]"
-
Set the password
user.set_password('name_of_the_new_password_for_user_selected')
-
Save the new password
"user.save()"
Start the server and log in using the username and the updated password.
- [Django]-Redirect to Next after login in Django
- [Django]-South migration: "database backend does not accept 0 as a value for AutoField" (mysql)
- [Django]-How to run a celery worker with Django app scalable by AWS Elastic Beanstalk?
13π
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.
- [Django]-Redirect to Next after login in Django
- [Django]-Django β No module named _sqlite3
- [Django]-*_set attributes on Django Models
12π
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'
- [Django]-Django: Implementing a Form within a generic DetailView
- [Django]-Django gunicorn sock file not created by wsgi
- [Django]-Embedding JSON objects in script tags
- [Django]-Is there a way to loop over two lists simultaneously in django?
- [Django]-Django development server reload takes too long
- [Django]-Sending an SMS to a Cellphone using Django
7π
You may try this:
1.Change Superuser password without console
python manage.py changepassword <username>
2.Change Superuser password through console
- [Django]-Django connection to postgres by docker-compose
- [Django]-TypeError: data.forEach is not a function
- [Django]-In django, how do I sort a model on a field and then get the last item?
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;
- [Django]-How do I deploy Django on AWS?
- [Django]-ImportError: Failed to import test module:
- [Django]-Switching to PostgreSQL fails loading datadump
5π
If you forgot create admin user first build one with createsuperuser
command on manage.py
then change the password.
- [Django]-Django: how to do calculation inside the template html page?
- [Django]-Django: Fat models and skinny controllers?
- [Django]-Cannot set Django to work with smtp.gmail.com
5π
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()
- [Django]-Timestamp fields in django
- [Django]-How to format time in django-rest-framework's serializer?
- [Django]-How can I create a deep clone of a DB object in Django?
4π
If youβre using custom user model
(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()
- [Django]-Explicitly set MySQL table storage engine using South and Django
- [Django]-Suddenly when running tests I get "TypeError: 'NoneType' object is not iterable
- [Django]-Django β how to unit test a post request using request.FILES
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
- [Django]-How to pass django rest framework response to html?
- [Django]-Django β how to visualize signals and save overrides?
- [Django]-Serializer call is showing an TypeError: Object of type 'ListSerializer' is not JSON serializable?
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.
- [Django]-Django: Safely Remove Old Migrations?
- [Django]-Removing 'Sites' from Django admin page
- [Django]-What's the idiomatic Python equivalent to Django's 'regroup' template tag?
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.
- [Django]-Factory-boy create a list of SubFactory for a Factory
- [Django]-Django: Use of DATE_FORMAT, DATETIME_FORMAT, TIME_FORMAT in settings.py?
- [Django]-Why does django run everything twice?
1π
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.
- [Django]-Django: how to do calculation inside the template html page?
- [Django]-How to loop over form field choices and display associated model instance fields
- [Django]-Django celery task: Newly created model DoesNotExist
1π
Just type this command in your command line:
python manage.py changepassword yourusername
- [Django]-How to produce a 303 Http Response in Django?
- [Django]-How can i test for an empty queryset in Django?
- [Django]-How to access request body when using Django Rest Framework and avoid getting RawPostDataException
0π
use
python manage.py dumpdata
then look at the end you will find the user name
- [Django]-Django β "Incorrect type. Expected pk value, received str" error
- [Django]-Django-Bower + Foundation 5 + SASS, How to configure?
- [Django]-How to use regex in django query
- [Django]-AccessDenied when calling the CreateMultipartUpload operation in Django using django-storages and boto3
- [Django]-How do Django models work?
- [Django]-Images from ImageField in Django don't load in template
0π
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
- [Django]-Adding django admin permissions in a migration: Permission matching query does not exist
- [Django]-How to solve "Page not found (404)" error in Django?
- [Django]-How to test "render to template" functions in django? (TDD)
-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.
- [Django]-Using Cloudfront with Django S3Boto
- [Django]-Http POST drops port in URL
- [Django]-Storing an Integer Array in a Django Database