app:
migrations 数据修改表结构
admin Django为我们提供的后台管理
apps 配置当前app
models ORM,写指定的类 通过命令可以创建数据库结构
tests 单元测试
views 业务代码
-
接下来在
- 修改models.py 文件:
from django.db import models
# Create your models here.
class UserType(models.Model):
name = models.CharField(max_length=32)
class UserInfo(models.Model):
username = models.CharField(max_length=32)
pwd = models.CharField(max_length=32)
email = models.CharField(max_length=32)
user_type = models.ForeignKey(UserType)
-
然后在
- 修改settings.py 文件:
# Application definition
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'cmdb',
]
- 然后在Terminal 运行以下命令:
(python manage.py makemigrations)
(python manage.py migrate)
(python3) C:\Users\Administrator\PycharmProjects\s14django>python manage.py makemigrations
Migrations for 'cmdb':
cmdb\migrations\0001_initial.py:
- Create model UserInfo
- Create model UserType
- Add field user_type to userinfo
(python3) C:\Users\Administrator\PycharmProjects\s14django>python manage.py migrate
Operations to perform:
Apply all migrations: admin, auth, cmdb, contenttypes, sessions
Running migrations:
Applying contenttypes.0001_initial... OK
Applying auth.0001_initial... OK
Applying admin.0001_initial... OK
Applying admin.0002_logentry_remove_auto_add... OK
Applying contenttypes.0002_remove_content_type_nam
e... OK
Applying auth.0002_alter_permission_name_max_lengt
h... OK
Applying auth.0003_alter_user_email_max_length...
OK
Applying auth.0004_alter_user_username_opts... OK
Applying auth.0005_alter_user_last_login_null... OK
Applying auth.0006_require_contenttypes_0002... OK
Applying auth.0007_alter_validators_add_error_messages... OK
Applying auth.0008_alter_user_username_max_length... OK
Applying cmdb.0001_initial... OK
Applying sessions.0001_initial... OK
(python3) C:\Users\Administrator\PycharmProjects\s14django>
-
如下图:
-
接下来在:
在admin.py 修改代码如下(创建后台管理):
from django.contrib import admin
from cmdb import models
# Register your models here.
admin.site.register(models.UserInfo)
admin.site.register(models.UserType)
在在Terminal 运行以下命令:
- 创建超级管理员(python manage.py createsuperuser)
(python3) C:\Users\Administrator\PycharmProjects\s14
django>python manage.py createsuperuser
Username (leave blank to use 'administrator'): alex
Email address: 123@qq.com
Password: asdfzxcv
Password (again):asdfzxcv
-
然后运行
-
打开浏览器(http://127.0.0.1:8000/admin)得到下图:
-
输入u(alex)m(asdfzxcv) 登录如下图: