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. 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)

#Result: 0000000012 (10 digits in total, use 0 to supplement the insufficient ones)


#1. Define a class:
#Custom converter
class MyCon:
    #Write a regular expression. The name must be regex

    #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
#Write expression
urlpatterns = [


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


Back: ok


from django.urls import path,re_path,register_converter
from app01 import views
class MyCon:
    def to_python(self,value):
        return value
    def to_url(self,value):
        return "%04d" %value
urlpatterns = [   


from django.shortcuts import render,HttpResponse,reverse
def re_test(request,year):
    return HttpResponse('ok')

Return to / test/0009

III. append_lash


# 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)

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

Tags: Python Django

Posted on Wed, 04 Dec 2019 17:57:47 -0500 by ozone1