python django is a heavyweight server-side development framework, we can use it to develop some back-end services and websites
First, install django
pip install django==2.1.5
Second, create, start project
1, the command creates:
django-admin startproject project_name # Create a project cd project_name
python manager.py startapp user # 1 to create subprojects python manager.py startapp order # Create a subproject 2
django-admin command after the installation is complete there will be a django, django installation is successful if no django-admin commands, check the installation of python scripts directory of whether to join the environmental variables
Two, pycharm created:
File --- New Project --- Django, fill in the project name, application module name to the following:
After creating the command:
python manager.py runserver # starter project
Access to complete the run inside the browser 127.0.0.1:8000 you can see the project running
After the completion of projects and applications to create, and then create two directories, templates and static, templates put html files, static discharge static files, js and css files, directory structure like this, as shown below:
Three, django common configuration changes
Here are some common configuration, you need to modify (settings in the project directory)
INSTALLED_APPS = [ 'django.contrib.admin', 'django.contrib.auth', 'django.contrib.contenttypes', 'django.contrib.sessions', 'django.contrib.messages', 'django.contrib.staticfiles', 'user' #加入自己写的模块 ] #INSTALLED_APPS,这个是管里面有哪些子模块,user是咱们自己创建的模块,如果需要使用,就要加入到里面 TEMPLATES = [ { 'BACKEND': 'django.template.backends.django.DjangoTemplates', 'DIRS': [os.path.join(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', ], }, }, ] #TEMPLATES是配置模板的地方,要修改的是DIRS这个,修改成自己的templates目录 DATABASES = { 'default': { 'ENGINE': 'django.db.backends.mysql', #数据库引擎改为mysql 'NAME': 'db_name',#数据库名称 'USER': 'db_user',#用户 'PASSWORD': 'db_password',#密码 'HOST': '127.0.0.1',#ip 'PORT': '3306',#端口号 } } #DATABASES 是数据库的配置,这里默认使用的是sqlite数据库,如果要改成mysql的话,修改成上面写的;如有多个数据库,在下边加'default2':{}即可 #注意,使用mysql数据库的话,需要注意以下2点 #1、数据库使用mysql的话,需要安装pymysql模块 #2、在项目同名的文件夹的__init__.py文件里面加入 # import pymysql # pymysql.install_as_MySQLdb() LANGUAGE_CODE = 'zh-Hans' #LANGUAGE_CODE是语言,默认是英文的,这里改成中文 TIME_ZONE = 'Asia/Shanghai' #TIME_ZONE是时区,默认是标准时区的,这里改成中国的时区
USE_TZ = False #默认是true,改为false表示使用当前时区
STATICFILES_DIRS = ( os.path.join(BASE_DIR, 'static'), ) #静态文件的目录 #STATICFILES_DIRS是静态文件的目录,放一些css、js,static文件夹需要自己创建