How do I create a superuser account in Django 1.9.

2020-05-24 20:29发布

I am reading a book ("Learning Django Web Development" by Sanjeev Jaiswal and Ratan Kumar) on Django, but the book is based on an earlier version of Django (prior to version 1.9). In order to populate the database with tables, the book uses the syncdb command:

$ python manage.py syncdb

Then the book says that terminal will prompt you to create a superuser account.

the syncdb command is no longer used in Django version 1.9 and up. After some research, it seems as if the migrate command populates the databse with tables, but it does not prompt the creation of a superuser account. How can I do this in Django 1.9.6?

7条回答
萌系小妹纸
2楼-- · 2020-05-24 20:49

First we’ll need to create a user who can login to the admin site. Run the following command:

$ python manage.py createsuperuser

Enter your desired username and press enter. Username: admin

You will then be prompted for your desired email address:

Email address: admin@example.com

The final step is to enter your password. You will be asked to enter your password twice, the second time as a confirmation of the first.

Password: **********
Password (again): *********
Superuser created successfully.
查看更多
成全新的幸福
3楼-- · 2020-05-24 20:50

first run

$ django-admin startproject mysite 

in cmd prompt,then apply migration by

cd mysite

mysite:

python manage.py makemigrations

then

python manage.py migrate

after that

python manage.py createsuperuser
查看更多
迷人小祖宗
4楼-- · 2020-05-24 20:59

I think you want to run these commands:

python manage.py makemigrations creates migration files based on your models

python manage.py migrate will create the tables in your db based on the migration files created

(see docs for more details on database migrations)

python manage.py createsuperuser will create a superuser for your application in the database (docs)

查看更多
一纸荒年 Trace。
5楼-- · 2020-05-24 21:00

For Django 2

User.objects.create_superuser(username='name',email='email',password='password')

From the docs

create_superuser(username, email, password, **extra_fields)

Same as create_user(), but sets is_staff and is_superuser to True.

Which can be embedded in a script, called from a command line or triggered via an API

查看更多
Root(大扎)
6楼-- · 2020-05-24 21:01
$ python manage.py migrate
$ python manage.py createsuperuser

https://docs.djangoproject.com/en/1.9/ref/django-admin/

查看更多
迷人小祖宗
7楼-- · 2020-05-24 21:10

$ python manage.py createsuperuser

It will ask username and password

http://127.0.0.1:8000/admin/ see

https://www.tutorialshore.com/create-new-admin-user-in-django/

查看更多
登录 后发表回答