django-> basic operations and new projects common configuration

First, install django

pip install django == 2.1.5 -U # install django / upgrade to the latest version


Second, create, start django project

django-admin startproject mjz # create a django project
cd mjz # into the project directory
python manange.py runserver # run
django-admin command after the installation is complete there will be a django, django installation is successful if no django-admin commands, check python scripts directory under the installation directory whether to join the environmental variables

Run to completion in the browser access 127.0.0.1:8000 you can see the normal operation of the project, as shown below

Note: The page currently showing in English, you need to change the language configuration file (LANGUAGE_CODE = 'zh-Hans'
#LANGUAGE_CODE is the language, the default is in English, here into Chinese)

 

 

 

Third, create an application

python manage.py startapp user # Create a user module
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 is as follows Figure:

 

 

 

 

 

 

Four, django common configuration changes (modify the configuration file: / learn / Interface Automation /mjz/mjz/settings.py)

Here are some common configuration need to be amended

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的话,修改成上面写的
#注意,使用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是时区,默认是标准时区的,这里改完中国的时区


STATICFILES_DIRS = (
os.path.join(BASE_DIR, 'static'),
) #静态文件的目录
#STATICFILES_DIRS是静态文件的目录,放一些css、js,static文件夹需要自己创建

MEDIA_ROOT = os.path.join(BASE_DIR,'static') #上传文件的路径

Guess you like

Origin www.cnblogs.com/wangyajuanjuan/p/12113019.html