Python 如何在 Django 1.9.6 中创建超级用户帐户

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/37618059/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 19:39:37  来源:igfitidea点击:

How do I create a superuser account in Django 1.9.6

pythondjangoweb-deployment

提问by Zach W

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:

我正在阅读一本关于 Django 的书(Sanjeev Jaiswal 和 Ratan Kumar 的“Learning Django Web Development”),但这本书基于 Django 的早期版本(1.9 版之前)。为了用表填充数据库,本书使用了 syncdb 命令:

$ 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?

在 Django 1.9 及更高版本中不再使用 syncdb 命令。经过一些研究,似乎 migrate 命令用表填充数据库,但它不会提示创建超级用户帐户。我怎样才能在 Django 1.9.6 中做到这一点?

回答by ben432rew

I think you want to run these commands:

我想你想运行这些命令:

python manage.py makemigrationscreates migration files based on your models

python manage.py makemigrations根据您的模型创建迁移文件

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

python manage.py migrate将根据创建的迁移文件在您的数据库中创建表

(see docsfor more details on database migrations)

(有关数据库迁移的更多详细信息,请参阅文档

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

python manage.py createsuperuser将在数据库中为您的应用程序创建一个超级用户 ( docs)

回答by James Fenwick

回答by Sandeep Kumar

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

输入您想要的用户名,然后按 Enter。用户名:管理员

You will then be prompted for your desired email address:

然后系统会提示您输入所需的电子邮件地址:

Email address: [email protected]

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.

回答by Unicorn Tears

For Django 2

对于 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.

create_superuser(用户名,电子邮件,密码,**extra_fields)

与 create_user() 相同,但将 is_staff 和 is_superuser 设置为 True。

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

可以嵌入到脚本中,从命令行调用或通过 API 触发

回答by benefits

$ python manage.py createsuperuser

$ python manage.py createsuperuser

It will ask username and password

它会询问用户名和密码

http://127.0.0.1:8000/admin/see

http://127.0.0.1:8000/admin/

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

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

回答by ansu

first run

第一次运行

$ django-admin startproject mysite 

in cmd prompt,then apply migration by

在 cmd 提示符下,然后通过以下方式应用迁移

cd mysite

mysite:

我的网站:

python manage.py makemigrations

then

然后

python manage.py migrate

after that

在那之后

python manage.py createsuperuser

回答by ansu

you create superuser with this :

你用这个创建超级用户:

python manage.py createsuperuser

this will create superuser for you and you can create many superuser . but notice before you can make super user you must run these command :

这将为您创建超级用户,您可以创建许多超级用户。但请注意,在您成为超级用户之前,您必须运行以下命令:

python manage.py makemigrations

and them :

还有他们 :

python manage.py migrate