day47 Django acquaintance, URLs routing and view function
wsgi
wsgi, web service gateway interface, network services gateway interface, is the format required to communicate data between applications and server programs
Dynamic pages can be returned by way of rendering a template, can be used in Python jinja2
module
Rendering templates:
Templates - html file
rendering - string replacement
MVC model and MTV
MVC模式,所谓MVC就是把Web应用分为模型(M),控制器(C)和视图(V)
MTV:
M: model 数据库相关操作
T:template 模板(html)相关操作
V:views 视图(业务逻辑,函数--类)
+ urls(url控制器)
django
Command to create a project
Download and install
pip install django==1.11.9
Create a project (to find a folder, switch to the folder with the following instruction)
django-admin startproject 项目名称
Create applications using the cd command to switch to the folder where the project file, using the Python runtime manage.py
in the startapp
method
python manage.py startapp 应用名称
In the project of the same name and the file inside the folder settings
to find the configuration file in the following configuration:
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'app01', #加上咱们项目文件夹下面的这个应用名称,将项目和应用关联到一起
]
Startup project:
python manage.py runserver 127.0.0.1:8001
#如果端口不写,默认是8000
#如果ip地址和端口号都不写,默认是127.0.0.1:8000
pycharm IDE project creation process django
Document describes the project directory
Files project folder:
- manage.py ----- Django project inside the tool that can be called django shell and databases, project start close interaction with the projects, whether you will divide the framework of several documents, there must be a boot file, in fact, their own It is a file.
- settings.py ---- contains the default settings of the project, including database information, debugging flags and variables other work.
- urls.py ----- responsible for URL patterns mapped to the application.
- wsgi.py ---- runserver command module on the use of wsgiref do a simple web server, see later renserver command, all content related to the socket in this file inside, and now do not need to pay attention to it.
urls route
urlpatterns = [
# 循环进行匹配,一旦匹配成功,就会执行后面的函数,并且不再往下面进行匹配了
url(r'^admin/', admin.site.urls), #第一个参数:正则 第二个参数:视图函数
url(r'^home/', views.home),
]
Response method
# render回复html页面
# HttpResponse 回复字符串
url wording
Unknown group
urls route wording:
url(r'^books/(\d+)/(\d+)/', views.books), #url无名分组,也可以叫做无名参数
View function writing:
def books(request,y,n):pass # y:拿到的是第一个参数,n:拿到的是第二个参数
Parameter passing mode is the location of mass participation
Famous grouping
urls route wording:
url(r'^books/(?P<year>\d+)/(?P<month>\d+)/', views.books)
View function writing:
def books(request,year,month):pass # year和month拿到的正则有名分组匹配到的那个名字相同的参数
Parameter passing mode is the keyword mass participation, regardless of the order parameter, but the name must be identical and regular
def books(request,y,n=None):pass # n=None就是默认值,当url正则匹配不到数据时,使用默认值,匹配到了的话,使用匹配结果
Note: The capture of the parameters are always strings
View function
The request object
request.path #request.path当前请求路径
request.method #当前请求方法(get,post...)
request.GET # 获取所有get请求携带过来的数据
request.POST # 获取所有post请求携带过来的数据
request.body # 获取所有post请求携带过来的数据的原始格式
Post verification request, remember to change what configuration settings in the configuration file
MIDDLEWARE = [
'django.middleware.security.SecurityMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
# 'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
]