The previous blogs have introduced the detailed use procedures in Django. Our commands are all integrated and the data is added for testing.
Project preparation
-
Create project
django-admin startproject bookmanager
-
Create application
python manager.py startapp book
-
Replace python interpreter: select as needed
# 进入指定虚拟环境 which python # python2 /home/python/.virtualenvs/py_django/bin/python # python3 /home/python/.virtualenvs/py3_django/bin/python
-
Install the app
INSTALLED_APPS = [ 'django.contrib.admin', 'django.contrib.auth', 'django.contrib.contenttypes', 'django.contrib.sessions', 'django.contrib.messages', 'django.contrib.staticfiles', #添加子应用 'book.apps.BookConfig' ]
-
Localization
#设置中文 LANGUAGE_CODE = 'zh-Hans' #亚洲上海时区 TIME_ZONE = 'Asia/Shanghai'
-
Template path
在应用同级目录下,创建templates 模板文件夹
,Then create the path
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', ], }, }, ]
-
Match urls in the project
Regular: As long as the path is not admin /, the match is successful. And include urls.py in the app
from django.conf.urls import url,include from django.contrib import admin urlpatterns = [ url(r'^admin/', admin.site.urls), #正则为:只要不是 admin/ 就算匹配成功 url(r'^',include('book.urls')) ]
-
Match urls.py in the application
Create urls.py in the application
Regular: the path contains booklist / , then call the corresponding bookList function in the view
from django.conf.urls import url from book.views import bookList urlpatterns = [ # 匹配书籍列表信息的URL,调用对应的bookList视图 url(r'^booklist/$',bookList) ]
-
Prepare view
# 定义视图:提供书籍列表信息 def bookList(request): return HttpResponse('OK!')
-
Turn on the server, test the project
# 进入项目文件中, 开启项目对应的服务器 python manage.py runserver # 浏览器中输入网址 http://127.0.0.1:8000/booklist/
Configuration
The connection configuration information of the database is saved in settings.py, and the initial configuration of Django uses the sqlite database by default.
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
}
}
-
To use the MySQL database, you need to install the driver first
使用MySQL数据库首先需要安装驱动程序
-
Add the following statement to the __init__.py file in the subdirectory of the Django project with the same name
import pymysql pymysql.install_as_MySQLdb()
The role is to allow Django's ORM to call PyMySQL with mysqldb.
-
Modify DATABASES configuration information
DATABASES = { 'default': { 'ENGINE': 'django.db.backends.mysql', 'HOST': '127.0.0.1', # 数据库主机 'PORT': 3306, # 数据库端口 'USER': 'root', # 数据库用户名 'PASSWORD': 'mysql', # 数据库用户密码 'NAME': 'book' # 数据库名字 } }
-
Create a database in MySQL
create database book charset=utf8;
Then it is to define the model, so the created usage flow is OK, if you want to see the next model class, please see the next article.