[Django] View System --2019-08-07 10:48:28

Original: http://106.13.73.98/__/36/

@
***
a view function (class), referred to as a view, Python is a simple function (class) that accepts the request and returns Web Web response.

The response may be a page's HTML content, a redirect, a 404 error, an XML document, or a picture.

No matter what the view itself contains logic, it must return a response. Write the code where it does not matter, as long as it is in your current directory project. In addition there is no more required, and can say "nothing magical place."

For code to be placed unified position, it agreed to a vulgar place the view in the project (project) or application (app) directory named views.py file.

A simple view:

# 以HTML文档的形式返回当前日期和时间
from django.http import HttpResponse
import datetime

def current_datetime(request):
    now = datetime.datetime.now()
    html = '<html><body>It is now %s.</body></html>' % now
    return HttpResponse(now)

Let us explain the above code line by line:

  1. First, we import the class HttpResponse, and Python's datetime library from Django.http module.
  2. Next, we define current_datetime function, which function is called views, each view function applies HttpRequest object as the first parameter, and is commonly known == request ==.
  3. This view will return a Httpresponse object that contains the generated response. == Each view function is responsible for returning an HttpResponse ==.

Django uses request and response objects passing through the system state
.
When a browser requests a page from the server, creating a Django HttpRequest object that contains metadata about the request. Then, Django loads the appropriate view, this view HttpRequest object to function as the first parameter
.
Each view is responsible for returning an HttpResponse.

FBV and CBV

FBV (function based view) view function based on
the CBV (class based View) based on the view class.

For the same logic, FBV and CBV wording:
the FBV

from django.shortcuts import render, redirect

def classes_add(request):
    if request.method == 'POST':
        add_classes_name = request.POST.get('add_classes_name')  # 提取班级名称
        Classes.objects.create(name=add_classes_name)  # 将新建班级写入数据库
        return redirect('/classes/')  # 跳转至展示页面
    return render(request, 'classes_add.html')  # 返回添加页面

# urls文件
url(r'^classes_add/$', views.classes_add),

CBV

from django.shortcuts import render, redirect
from django.views import View

class ClassesAdd(View):
    # 可自定义允许的请求
    # http_method_names = ['get', 'post']

    # 处理get请求的逻辑
    def get(self, request):
        return render(request, 'classes_add.html')  # 返回添加页面

    # 处理post请求的逻辑
    def post(self, request):
        add_classes_name = request.POST.get('add_classes_name')  # 提取班级名称
        Classes.objects.create(name=add_classes_name)  # 将新建班级写入数据库
        return redirect('/classes/')  # 跳转至展示页面

# urls文件
url(r'^classes_add/$', views.ClassesAdd.as_view()),


Add to the view decorator

FBV itself is a function, so the function and added to the general decorator no difference.

Decorator decoration CBV

Method class independent functions are not identical, the method can not be directly applied to a function of decorator class, we need a method to convert it to decorators.
== provides the Django method_decorator decorators for decorative function The method converts to a decorator. ==

Decorator function is as follows:

import time

def wrapper(fn):
    def inner(*args, **kwargs):
        start_time = time.time()
        ret = fn(*args, **kwargs)
        end_time = time.time()
        print("used:", end_time - start_time)
        return ret
    return inner

Decorative CBV worded as follows:

from django.shortcuts import render, redirect
from django.views import View
# 导入method_decorator
from django.utils.decorators import method_decorator

class ClassesAdd(View):

    # 处理get请求的逻辑
    @method_decorator(wrapper)  #!
    def get(self, request):
        return render(request, 'classes_add.html')  # 返回添加页面

    # 处理post请求的逻辑
    @method_decorator(wrapper)  #!
    def post(self, request):
        add_classes_name = request.POST.get('add_classes_name')  # 提取班级名称
        Classes.objects.create(name=add_classes_name)  # 将新建班级写入数据库
        return redirect('/classes/')  # 跳转至展示页面

About dispatch () method

It should be noted when using the CBV, the request will first come to perform dispatch () method, if you need a specific request for batch processing, such as get, post, and so do some of the operations when we can manually override this method in a subclass, such in fact, operation and add decorators on FBV the same effect.

from django.shortcuts import render, redirect
from django.views import View

class ClassesAdd(View):

    # 可自定义处理请求前后的逻辑
    def dispatch(self, request, *args, **kwargs):
        # 处理请求之前的逻辑
        ret = super().dispatch(request, *args, **kwargs)
        # 处理请求之后的逻辑
        return ret

    # 处理get请求的逻辑
    def get(self, request):
        return render(request, 'classes_add.html')  # 返回添加页面

    # 处理post请求的逻辑
    def post(self, request):
        add_classes_name = request.POST.get('add_classes_name')  # 提取班级名称
        Classes.objects.create(name=add_classes_name)  # 将新建班级写入数据库
        return redirect('/classes/')  # 跳转至展示页面


Request object Response object

The request object

When a page is requested, Django creates an HttpRequest object of this request contains the original message.
The Django This object will be automatically passed to the appropriate function of the view, the general view function using convention == request == receiving the object parameters.
Official documents
***

Request related properties

Attributes Explanation
path_info Returns the user to access the url, and domain name without content? Back
method The method of HTTP request string representation used, all uppercase represents
GET Dictionary-like object containing all HTTP GET parameters
POST Dictionary-like object containing all the HTTP POST parameters
body Request body, byte type, request.POST data is extracted from inside the body to

Other property

All properties should be considered read-only, unless otherwise noted.

The row Django Request packets, the header information, relating to the contents of the package into HttpRequest class attributes.
In addition to the special instructions, others are read-only.
.
== == scheme
string representing the request scheme (usually http or HTTPS)
.
== == body
a string representing the request message body. Very useful when dealing with non-HTTP form of messages, such as: binary files, XML, Json and so on.
But if you want to process form data, it is recommended that POST.
In addition, we can also use the class file method of Python to operate it, in detail reference request.read () method.
.
== == path
a string representing the request path components (excluding the domain name and content? later).
.
== == encoding
encoding a string that represents the data submitted mode.
If None, the representation is used DEFAULT_CHARSET set, the default is "utf-8".
this property is writable, you may be changed coded access form data used by modifying it, after modifications are any access attribute value will use the new encoding (e.g., reading data from the GET or POST).
If it is determined not DEFAULT_CHARSET form data encoding, it is used.
.
== == POST
a similar object dictionary, if the request is included in the form data , then packaged into QueryDict data objects.
POST POST request with an empty dictionary can be - sent via HTTP POST method does not have any data forms, it still creates QueryDict object.
Therefore, we use if request.method == 'POST':to determine whether you are using the POST method instead of using if request.method:
Additionally, if you use POST uploaded file, the file information will be included in fILES properties.
.
== == COOKIES
a standard Python dictionary containing all of the cookie, keys and values are strings.
.
== == fILES
a dictionary-like object containing all the upload information.
fILES each bond is <input type = "file" name = "" /> in the name, the value compared with the corresponding data.
Note that the method is only on request fILES POST and case <form> form submitted with enctype = "multipart / form-data " will be included in the data. Otherwise, FILES will be a blank dictionary-like object.
.
== == the session
object is similar to a dictionary to read but also write, indicating the current session.
Only when Django enable support sessions available.
see the documentation for complete details about the session.

== META ==
a standard Python dictionary containing all header HTTP.
Specific header information depends on the client and server.
.
CONTENT_LENGTH - the length of the body of the request (a string).
MIME type text request - CONTENT_TYPE.
HTTP_ACCEPT - Content-Type Response receivable.
HTTP_ACCEPT_ENCODING - response may be received encoded.
HTTP_ACCEPT_LANGUAGE - response may be received language.
HTTP_HOST - HTTP Host header sent by customer.
HTTP_REFERER - Referring page.
HTTP_USER_AGENT - client user-agent string.
QUERY_STRING - single string in the form of a query string (not parsed form).
REMOTE_ADDR - IP address of the client.
REMOTE_HOST - host name of the client.
REMOTE_USER - after user authentication server.
REQUEST_METHOD - a string, such as "GET" or "POST".
SERVER_NAME - host name of the server.
SERVER_PORT - server port (a string).
.
When seen from the above, and in addition CONTENT_LENGTH CONTENT_TYPE, any HTTP request header is converted to META keys
are all capital letters and finally replaced with an underscore connector plus HTTP_ prefix.
So called X-Bender converted into a head in the META HTTP_X_BENDER bond.

== user ==
object AUTH_USER_MODEL type, indicates the user currently logged in.
If there is no user login, the user will be set to an instance of django.contrib.auth.models.AnonymousUser.
can be distinguished by is_authenticated ().
example:

if request.user.is_authenticated():
    # Do something for logged-in users.
else:
    # Do something for anonymous users.

When the user only when Django middleware enabled AuthenticationMiddleware available.
.
Anonymous User: classes models.AnonymousUser
of django.contrib.auth.models.AnonymousUser Django.contrib.auth.models.User class implements an interface, but with a few different below point:
.
. the above mentioned id is always None
. username will always be an empty string
. get_username () always returns an empty string
. is_staff and is_superuser is always False
. is_active is always False
. Groups and user_permissions always empty
is_anonymous () returns True instead . False
is_authenticated () returns False instead of True.
set_password (), check_password (), the Save (), the Delete () will throw an error NotImplementedError
new in Django 1.8:
new AnonymousUser.get_username () to better simulate django. contrib.auth.models.User.

Upload file Example:

def upload(request):
    if request.method == 'POST':
        file_obj = request.FILES.get('file')  # 获取文件对象(获取文件信息要用FILES)
        upload_file_path = os.path.join(settings.BASE_DIR, 'upload_file', file_obj.name)  # 文件上传路径, file_obj.name为str类型
        # 判断文件是否存在的逻辑
        if os.path.exists(upload_file_path):
            name, suffix = file_obj.name.split('.')  # 切割文件名与文件后缀
            file_name = name + "_文件副本1." + suffix  # 重新拼接文件名
            upload_file_path = os.path.join(settings.BASE_DIR, 'upload_file', file_name)  # 文件上传路径
            # 判断文件是否有副本的逻辑
            if os.path.exists(upload_file_path):
                file_list = os.listdir(os.path.dirname(upload_file_path))
                # 获取所有副本文件
                file_copy_num = []
                for i in file_list:
                    if name not in i: continue
                    copy_num = i.split('.')[0][-1]  # 提取副本数字
                    if copy_num == name[-1]: continue
                    file_copy_num.append(copy_num)
                file_copy_max = int(max(file_copy_num))  # 提取最大的副本数字
                file_name = name + "_文件副本" + str(file_copy_max + 1) + '.' + suffix
                upload_file_path = os.path.join(settings.BASE_DIR, 'upload_file', file_name)  # 文件上传路径
        with open(upload_file_path, 'wb') as f:
            [f.write(chunk) for chunk in file_obj.chunks()]  # 从文件里一点一点的读数据,写到文件里
        return HttpResponse("文件上传成功!")  # 返回提示信息
    return render(request, 'upload.html')  # 返回上传页面

method

get_host == ==
.
According to the HTTP_X_FORWARDED_HOST (if open USE_X_FORWARDED_HOST, default False) HTTP_HOST header information and returns the requested original host.
.
If the two headers do not provide the appropriate values, and then using SERVER_NAME SERVER_PORT, are described in detail in PEP 3333.
.
USE_X_FORWARDED_HOST: A Boolean value that specifies whether to prioritize the use of the header X-Forwarded-Host, only in the case of proxy settings of the header, can be used.
.
For example: 127.0.0.1: 8000
.
Note: When the host is located behind the plurality of agents, get_host () method will fail. Unless you use the header rewrite the middleware proxy.

get_full_path == ==
.
returns the full path of the request.
For example: / list / test / name = zyk?

get_signed_cookie == (Key, default = RAISE_ERROR, = Salt '', the max_age = None) ==
.
returns Cookie value corresponding to the signed if the signature is not valid django.core.signing.BadSignature returned.
.
If you supply default parameters, it will not throw an exception and returns the value of default.
.
Optional parameters salt can be used to provide additional protection for the security key brute force attack. max_age Cookie parameter for checking the timestamp corresponding to a time to ensure that no more than max_age Cookie seconds.

>>> request.get_signed_cookie('name')
       'Tony'
>>> request.get_signed_cookie('name', salt='name-salt')
       'Tony' # 假设在设置cookie的时候使用的是相同的salt
>>> request.get_signed_cookie('non-existing-cookie')
        ...
        KeyError: 'non-existing-cookie'    # 没有相应的键时触发异常
>>> request.get_signed_cookie('non-existing-cookie', False)
        False
>>> request.get_signed_cookie('cookie-that-was-tampered-with')
        ...
        BadSignature: ...   
>>> request.get_signed_cookie('name', max_age=60)
...
SignatureExpired: Signature age 1677.3839159 > 60 seconds
>>> request.get_signed_cookie('name', False, max_age=60)
        False

is_secure == () ==
.
If the request is safe, then it returns True, that is initiated through HTTPS request.

is_ajax == ==
.
If the request is initiated by the XMLHttpRequest, it returns True, HTTP_X_REQUESTED_WITH by checking whether the respective header string 'XMLHttpRequest'.
.
Most modern JavaScript libraries will be sent to this head. If you write your own XMLHttpRequest call (on the browser side), you must manually set this value to make is_ajax () can work.
.
If a response is needed based on whether the request is initiated by AJAX, and you are using some form of caching such as Django's cache middleware, your view you should use vary_on_headers ( 'HTTP_X_REQUESTED_WITH') decorated to allow the response to be cached correctly.

NOTE: When a plurality of key-value pairs (e.g., checkbox input type), because of the use GetList () method.

response object

Compared with the HttpRequest object is created automatically by Django, HttpResponse objects are our responsibilities, and we write each view will need to be instantiated, fill and return an HttpResponse.
HttpResponse class located django.http module.

Pass a string:

from django.http import HttpResponse
response = HttpResponse("Here's the text of the Web page.")
response = HttpResponse("Text only, please.", content_type="text/plain")

Setting or deletion response message header:

response = HttpResponse()
response['Content-Type'] = 'text/html; charset=UTF-8'
del response['Content-Type']

Attributes

  • HttpResponse.content: response content
  • HttpResponse.charset: response code content
  • HttpResponse.status_code: status of the response it

    JsonResponse objects

    JsonResponse is HttpResponse subclass, designed to generate a response JSON-encoded.

from django.http import JsonResponse

def test(request):
    dct = {'name': 'zyk', 'sex': 'boy'}
    response = JsonResponse(dct)
    print(response.content)
    return response

The default will only transfer data dictionary type, if you want to pass Nondictionary types of data need to set the parameters of safe = False:

response = JsonResponse([1, 2, 3], safe=False)

Original: http://106.13.73.98/__/36/

Guess you like

Origin www.cnblogs.com/gqy02/p/11313915.html