V. custom converter, httprequest image alignment, HttpResponse, JsonRepons of view layer

I. virtual environment 1. Create with Python - > files - > new project - > select virtual environment 2 Settings > project creation 3. Cr...

I. virtual environment

1. Create with Python - > files - > new project - > select virtual environment 2 Settings > project creation 3. Create from the command line. See https://www.cnblogs.com/liuqingzheng/p/9508851.html for details

2. Differences between django 2.0 and django 1.0 routing layers

(**** URL, data separated by re path grouping, is a string) -re_path: same as url usage of 1.0 -Path: the path passed is the exact path -Re path ----- > equal to the original url (re path module) 5 converters -- > path ('test / < path: year > ', views. Re? Test), str, matching non empty strings other than path separator (/), which is the default form int, matches a positive integer, containing 0. slug, which matches letters, numbers, and a string of horizontal bars and underscores. uuid, matching the formatted uuid, such as 075194d3-6885-417e-a8a8-6c931e272f00. Path, which matches any non empty string, contains the path separator (/) (can't use?)

-Custom converter (Django 2.0)

print('%010d'%12) #Result: 0000000012 (10 digits in total, use 0 to supplement the insufficient ones)

urls.py

#1. Define a class: #Custom converter class MyCon: #Write a regular expression. The name must be regex regex='[0-9]' #The matched data will be sent here, and the retrun will be received by the view function def to_python(self,value): return int(value) # ××× # return value #String type #For reverse analysis def to_url(self,value): return "%04d" %value #2 import module from django.urls import register_converter #Register and converter register_converter(MyCon,'yyy') #Write expression urlpatterns = [ path('test/<yyy:year>',views.re_test), ]

views.py

def re_test(request,year): return HttpResponse('ok')

Visit: http://127.0.0.1:8000/test/2018

Back: ok

urls.py

from django.urls import path,re_path,register_converter from app01 import views class MyCon: regex='[0-9]' def to_python(self,value): return value def to_url(self,value): return "%04d" %value register_converter(MyCon,'yyy') urlpatterns = [ path('test/<yyy:year>',views.re_test,name='test'), ]

views.py

from django.shortcuts import render,HttpResponse,reverse def re_test(request,year): url=reverse('test',args=(9,)) print(url) return HttpResponse('ok')

Return to / test/0009

III. append_lash

settings.py

#http://127.0.0.1:8000/test will not complete the following '/' for test when it is set to False, and it will become 404 when the web page is opened (if there is no such page) APPEND_SLASH=False

IV. http request pairs in view layer

#request property # https://www.cnblogs.com/liuqingzheng/articles/9509801.html # def test(request): # print(type(request)) #Data from the foreground post is packaged in the post dictionary # request.POST #The data carried in the foreground browser window is packed into the GET dictionary # request.GET #How to request from the front desk # request.method #post submitted data, body content # request.body #Take out the requested path, unable to get the data part # print(request.path) #Take out the path of the request and get the data part # print(request.get_full_path()) #Data in the request header (e.g. request address, name, and address accessed from) # print(request.META) #Reference: from which link #post submit the data format and put it in the body # name=lqz&age=18&sex=1 # return HttpResponse("ok")

Add: debug breakpoint:

V. HttpResponse of view layer








Vi. JsonResponse object of view layer





















4 December 2019, 17:57 | Views: 6098

Add new comment

For adding a comment, please log in
or create account

0 comments