- 新建项目
django-admin startproject MyProject
- 进入MyProject根目录后,新建app:MyApp
python manage.py startapp MyApp
- 初始化-生成sqlite数据库表
python manage.py makemigrations python manage.py migrate
- 初始化-创建超级管理员
python manage.py createsuperuser
- 进入pycharm打开项目
- 初始化-settings.py
# settings.py # 初始化-访问权限 ALLOWED_HOSTS = ['*',] # 初始化-加载app INSTALLED_APPS = [ 'django.contrib.admin', 'django.contrib.auth', 'django.contrib.contenttypes', 'django.contrib.sessions', 'django.contrib.messages', 'django.contrib.staticfiles', 'Myapp' ] # 初始化-关闭csrf 避免报错 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', ] # 初始化-语言、时区 LANGUAGE_CODE = 'zh-hans' TIME_ZONE = 'Asia/Shanghai'
- 初始化-admin.py
import inspect import sys from django.contrib import admin # Register your models here. cls_members = inspect.getmembers(sys.modules[__name__], inspect.isclass) for name, cls in cls_members: admin.site.register(cls)
- 启动
python manage.py runserver
- 打开网址及显示效果
http://127.0.0.1:8000/
- 进入admin页面
http://127.0.0.1:8000/admin/