The path to learning in Python -2018/6/19

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.