The path to learning in Python -2018/6/19 1. Registering a custom converter
converts.py
class Birthday: =‘[0-9]{8}‘ # 匹配规则 def to_python(self, value): # 匹配的字符串返回具体的变量值,传递到对应的视图函数中 returnint(value) def to_url(self,value): # 反向解析 return"%04d"% value
urls.py
from django.urls import path, register_converterfrom app01 import views, urls, convertsregister_converter(converts.Birthday, "birth") urlpatterns = [ path(‘gyq/<birth:year>‘, views.birthday)]
views.py
def birthday(request, date): return HttpResponse(date)
2.HttpRequest objects
URL Composition: protocol/ip/port/path/request data
Request Common Properties
HttpRequest.GETContains all the parameters of the HTTP GET
HttpRequest.POSTIf the request contains form data, wrap the data into a Querylist object
HttpRequest.methodMethod of HTTP request (Get/post)
HttpRequest.encodingData encoding for Submissions
Request Common methods
HttpRequest.get_full_path()Returns the path, including the query string.
HttpRequest.get_full_path()Determine if the request was initiated by XMLHttpRequest
View Response Object
There are three main types of response objects:
- HttpResponse ()
- Render ()
- Redirect ()
HttpResponse () in parentheses directly with a specific string as the response body
Render ()
render(request, template_name[, context])
Request: The requested object used to generate the response.
Template_name: The full name of the template to use, such as index.html
Context: A dictionary that is added to the template context. The default is an empty dictionary. If a value in the dictionary is callable, the view is called before the template is rendered.
The path to learning in Python -2018/6/19