Python framework of Django (routing system URL, view function views)

1. Routing system (URL)

1. URL configuration (URLconf) is like the directory of the website supported by Django. Its essence is the mapping table between the URL pattern and the view function to be called for the URL pattern.
Simply put, we tell Django in this way to call this code for this URL and call that code for that URL.

urlpatterns = [
    url(正则表达式, views视图函数,参数,别名),
]
  • Regular expression: the regularity of the URL path
  • views view function: usually a view function or a string specifying the path of the view function
  • Parameters: optional default parameters (dictionary form) to be passed to the view function
  • Alias: Optional name parameter, aliases used in other calls

2. For example:

from django.conf.urls import url
from . import views
  
urlpatterns = [
    url(r'^blog/(?P<year>[0-9]{4})/$', views.year_archive, {
    
    'jiu': 'ok'}),
]

3. Unnamed group

	url('article/(\d{4})/(\d{2})', views.article_year),
    # 表示article后可匹配任意4个数字,都能打开该页面
    # 加()将正则内容分组,就可以获取到对应的值了,就是说页面的网址也会跟着变成输入的那4个数字
    # 简单来说就是一个分组会对应article_year函数的一个参数

4. Famous group

# 有名分组:分别起名为year和month,这样对应的形参也必须同名
    url(r'article/(?P<year>\d{4})/(?P<month>\d{2})', views.article_year),
    url(r'register/', views.register, name="reg"),  # reg是这个地址的别名,在其他文件里都可以使用

5. Route distribution (include)

# 自动路由分发,要导入include
    path('blog/', include('blog.urls')),
    # 名字为urls,因此需要在blog目录下建立一个urls文件,将这些需要分发到一堆的路径都粘贴进去
    # 意思就是以blog开头的内容,全都到blog.urls里面去找

11

Two, view function (views)

1. Two core objects are generated in the http request :

  • http request: HttpRequest object
  • http response: HttpResponse object

2. Attributes and methods of HttpRequest (request)

  • path: the full path of the requested page, excluding the domain name
  • method: The string representation of the HTTP method used in the request. All caps
if  req.method=="GET":
	do_something()
	elseif req.method=="POST":
	do_something_else()
  • GET: dictionary-like object containing all HTTP GET parameters
  • POST: A dictionary-like object containing all HTTP POST parameters. It
    is also possible that the server receives an empty POST request, that is, the form passed

The HTTP POST method submits the request, but there may be no data in the form, so
if req.POST cannot be used to determine whether the HTTP POST method is used; you should use if req.method=="POST"

  • COOKIES: A standard Python dictionary object containing all cookies; keys and values ​​are both strings
  • FILES: A dictionary-like object that contains all uploaded files. Each Key in FILES is the tag in
    (1) filename: the name of the uploaded file, expressed as a string
    (2) content_type: Content Type of the uploaded file
    (3) content: Original content of uploaded file
  • user: represents the currently logged in user
  • session: the only readable and writable attribute, representing the dictionary object of the current session; this attribute is only available when you have activated session support in Django
  • request.POST.getlist(''): Commonly used methods to get content

3. HttpResponse (response)
is automatically created by django for the HttpRequest object, but the HttpResponse object must be created by ourselves.
Each view request processing method must return an HttpResponse object, and the HttpResponse class is in django.http.HttpResponse.

  • Page rendering: render (commonly used), render_to_response (less used)
	return render(req, "index.html", locals())
    # 参数:请求对象,要返回的页面,返回前端的全部键值对——这里表示{"time":t}
  • Page jump: redirect("path")
		user = req.POST.get("user")
        if user=="zahuw":
            return redirect("/login/")
        # 就相当于是http://127.0.0.1:8080/login/
        # 若改成:return redirect("login.html")
        # 这样跳转到页面url不会变,仍然是原来的url,一刷新就回到原来的页面了
  • Pass all the variables in the function to the template: locals()
	# return render(req,"index.html",{"action":li,"d":dic,"c":ani})  # 手动列出来
    return render(req,"index.html",locals())  # 全部获取

Guess you like

Origin blog.csdn.net/Viewinfinitely/article/details/106969476