MIDDLEWARE = [
'django.middleware.security.SecurityMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
# 'django.middleware.csrf.CsrfViewMiddleware', # 注释此项
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
]
DATABASES = {
# 'default': {
# 'ENGINE': 'django.db.backends.sqlite3',
# 'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
# }
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'userinfo',
'USER': 'root',
'PASSWORD': '123.com',
'HOST':'localhost',
'PORT':'3306',
}
}
# LANGUAGE_CODE = 'en-us'
LANGUAGE_CODE = 'zh-Hans'
# TIME_ZONE = 'UTC'
TIME_ZONE = 'Asia/Shanghai'
ALLOWED_HOSTS = ['*']
from django.db import models
class User(models.Model):
GENDER_CHOICES = (
('男', '男'),
('女', '女'),
)
name = models.CharField(max_length=30, unique=True, verbose_name='姓 名')
birthday = models.DateField(blank=True, null=True, verbose_name='生 日')
gender = models.CharField(max_length=30, choices=GENDER_CHOICES, verbose_name='性 别')
account = models.IntegerField(default=0, verbose_name='工 号')
age = models.IntegerField(default=18, verbose_name='年 龄')
简言之:在Django 1.9及未来的版本种使用migrate代替原先的syscdb.
先在Mysql中创建数据库"userinfo"
执行下面代码:(直接再Pycharm里的"Terminal"终端执行 )
(venv) F:\python1\UserSystem>python manage.py makemigrations
(venv) F:\python1\UserSystem>python ./manage.py migrate
from django.contrib import admin
from . models import User
class HostAdmin(admin.ModelAdmin):
list_display = [
'name',
'age',
'birthday',
'gender',
'account',
]
search_fields = ('name',)
admin.site.register(User, HostAdmin)
admin.AdminSite.site_header = '运维系统管理后台'
admin.AdminSite.site_title = '运维系统'
from django.contrib import admin
from django.urls import path
urlpatterns = [
path('admin/', admin.site.urls),
]
(venv) F:\python1\UserSystem>python ./manage.py runserver
(venv) F:\python1\UserSystem>python ./manage.py createsuperuser
用户名: accp
电子邮件地址:
Password:
Password (again):
密码长度太短。密码必须包含至少 8 个字符。
这个密码太常见了。
密码只包含数字。
Bypass password validation and create user anyway? [y/N]: y
Superuser created successfully.