Python版本:Python 3.6.4
Django版本:Django 2.0.2
pip install django
1、创建项目:
django-admin.py startproject projectname
2、创建APP:
django-admin.py startapp appname
3、配置mysql:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'mydatabase',
'USER': 'mydatabaseuser',
'PASSWORD': 'mypassword',
'HOST': '127.0.0.1',
'PORT': '3306',
}
}
需要安装pymysql库
pip install pymysql
需要在init.py配置:
import pymysql
pymysql.install_as_MySQLdb()
4、配置templates:
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [BASE_DIR+"/templates",],
'APP_DIRS': True,
'OPTIONS': {
'context_processors': [
'django.template.context_processors.debug',
'django.template.context_processors.request',
'django.contrib.auth.context_processors.auth',
'django.contrib.messages.context_processors.messages',
],
},
},
]
5、配置static:
STATIC_URL = '/static/'
STATICFILES_DIRS = (
os.path.join(BASE_DIR, 'static'),
)
6、导出数据库模型并替换原本的models.py文件:
python manage.py inspectdb > yourSiteDirectory/yourApplication/models.py
7、启动项目:
python manage.py runserver