一.重要概念
- app: is a Web application that does something. An app usually is composed of a set of models (database tables), views, templates, tests.
- project: is a collection of configurations and apps. One project can be composed of multiple apps, or a single app.
二.project常见配置
1.在settings.py文件中修改数据库连接:
'ENGINE': 'django.db.backends.mysql', # 连接引擎 'NAME': 'django', # 数据库名称 'USER': 'root', # 用户名 'PASSWORD': 'root', # 密码 'HOST': '127.0.0.1', # 地址 'PORT': '3306', # 端口
时间和时区设置:
LANGUAGE_CODE = 'zh-Hans' TIME_ZONE = 'Asia/Shanghai'
使用命令行,初始化数据库:
python manage.py migrate
报错如下:
django.core.exceptions.ImproperlyConfigured: Error loading MySQLdb module.
Did you install mysqlclient?
安装pymysql
pip3 install pymysql
安装完成后,在__init__.py中填写:
import pymysql
pymysql.install_as_MySQLdb()
运行python manage.py migrate命令,即可。
创建用户:
python manage.py createsuperuser
三.创建application
python manage.py startapp blog
或者在New Project时填写:application name.
设置satic_root
STATIC_ROOT = os.path.join(BASE_DIR, "static/") #请注意static前面不带/,而后面带/
运行命令,将静态文件汇集起来:
python manage.py collectstatic