Django 简明教程
Django Admin - Create User
Django 框架提供了一个可靠的用户管理系统。可以通过 admin interface 和编程方式来管理用户、组和权限。
The Django framework readily provides a robust user management system. The users, groups, and permissions are managed both through the admin interface as well as programmatically.
当使用 startproject command 创建一个新项目时, admin 和 auth 应用程序在 INSTALLED_APPS 中默认添加。所有用户对象都存储在 "django.contrib.auth.models.User" 模型中。
When you create a new project with the startproject command, the admin and auth apps are added in the INSTALLED_APPS by default. All the user objects are stored in the "django.contrib.auth.models.User" model.
假设使用以下命令创建了项目 −
Assuming that the project has been created with the following command −
django-admin startproject myproject
admin 和 auth 应用程序可在 settings 模块的 INSTALLED_APPS 列表中找到 −
The admin and auth apps are found in the list of INSTALLED_APPS in the settings module −
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
. . .,
]
你至少需要一个用户才能使用管理面板。用户对象有以下类型 −
You need at least one user to be able to use the admin panel. The User objects are of the following types −
-
Superuser − A user object that can log into the admin site and possesses permissions to add/change/delete other users as well as perform CRUD operations on all the models in the project, through the admin interface itself.
-
Staff − The User object has a is_staff property. When this property is set to True, the user can login to the Django admin site. The superuser is a staff user by default.
-
Active − All users are marked as "active" by default. A normal active user (without staff privilege) is not authorized to use the Admin site.
Creating a Superuser
可以通过两种方式创建超级用户。
A superuser can be created in two ways.
-
With createsuperuser parameter to manage.py script from the command line
-
Calling create_superuser() function programmatically.
命令行用相当简单。在项目父文件夹中执行以下命令 −
The command line use is straightforward. Execute following command from inside the project’s parent folder −
python manage.py createsuperuser
系统提示时输入 username 和 password −
Enter the username and password when prompted −
Username: admin
Email address: admin@example.com
Password: ********
Password (again): ********
The password is too similar to the username.
This password is too short. It must contain at least 8 characters.
This password is too common.
Bypass password validation and create user anyway? [y/N]: y
Superuser created successfully.
启动 Django 服务器,用超级用户凭据登录,开放管理员网站的登录页面 http://localhost:8000/admin/ 。
Start the Django server, and login with the superuser credentials, open the login page of admin site http://localhost:8000/admin/.
管理主页会显示两项模型组和用户。
The admin homepage shows the two models Groups and Users.
Creating a New User
要新建用户,单击“+”添加按钮。输入用户名(管理器)和符合规定的密码。
To create a new user, click the "+" Add button. Enter the Username (manager) and the password that complies with the rules as mentioned.
当新用户成功添加后,下拉启用员工状态,如果你想授予其登录权限。默认情况下,每个用户均已激活。你还可以输入其它详细信息,例如 name 和 email address 等。
After the new user is successfully added, scroll down to enable the staff status if you want to grant the privilege of logging in. By default, each user is Active. You can also enter the other details such as the name and email address, etc.
展开用户模型来查看所有用户。
Expand the Users model to see all the users.
要以编程方式创建用户,使用 Django Shell。
To create the user programmatically, use the Django shell.
像下面这样调用 create_user() 函数 −
Call the create_user() function as follows −
>>> from django.contrib.auth.models import User
>>> usr=User.objects.create_user('testusr', 'test@example.com', 'pass123')
为获得工作人员状态,将 is_staff 属性设为 True。
To accord the staff status, set the is_staff property to True.
>>> usr.is_staff=True
>>> usr.save()
刷新管理网站中的用户列表。你应该可以看到新添加的用户。
Refresh the users list in the admin site. You should see the newly added user.
一个 group 对用户进行分类,以便你可以将权限或一些其它标签应用到这些用户。 A user can belong to any number of groups 。
A group categorizes users so you can apply permissions, or some other label, to those users. A user can belong to any number of groups.
在 Django 中,一个 group 是已分配给一个或多个用户的权限列表。当你新建或修改用户时,简单地选择期望的组,以便其中列出的所有权限都将授予用户。此外,你始终可以除了通过用户获得的权限之外将其它权限添加到用户。
In Django, a group is a list of permissions to be assigned to one or more users. When you create or modify a user, simply choose the desired group, so that all the permissions listed in it will be accorded to the user. In addition, you can always add other permissions to a user in addition to those obtained from the user.
转到 admin site 的 homepage ,并单击 Groups 模型行中的 Add 按钮来添加一个名为 admins 的新组。从可用权限列表中选择要授予给此组中的用户的权限。
Go to the homepage of the admin site and click the Add button in Groups model row to add a new group called admins. Select the permissions to be accorded to the users in this group from the list of available permissions.
要为用户启用此组中包含的所有权限,修改其属性,下拉查看可用组,选择并保存。
To enable all the permissions included in this group to a user, modify his properties and scroll down to see the available groups, make the selection and save.
Django 的管理网站可以非常有效地用于创建和管理用户,以及管理组和权限。
Django’s admin site can be very effectively used to create and manage users as well as manage groups and permissions.