Rajeshwar
Rajeshwar

Reputation: 11681

Creating a super user that is not able to access admin site

I am creating an admin user using the following code. However this user is unable to login. Any suggestions on why this user cant log in to the admin site

  user = User.objects.create(username = "joe",first_name="jordan", last_name="...", email="[email protected]", password="admin123",is_staff=True,is_superuser=True)

The username is joe and password is admin123 however this user cant login

Upvotes: 1

Views: 62

Answers (1)

neverwalkaloner
neverwalkaloner

Reputation: 47374

Django stores hashed passwords. Since you passed to DB plain password, Django could not authenticate user. You should use set_password when creating new user to hash password:

user = User(username = "joe",first_name="jordan", last_name="...", email="[email protected]",is_staff=True,is_superuser=True)
user.set_password("admin123")
user.save()

Or you can use create_superuser method:

user = User.objects.create_superuser(username = "joe",first_name="jordan", last_name="...", email="[email protected]", password="admin123")

Upvotes: 3

Related Questions