start with django

一.重要概念

  • 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

文章出处:www.cnblogs.com/jizhong

本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接。否则保留追究法律责任的权利。

原文地址:https://www.cnblogs.com/jizhong/p/15179354.html