[Four] Django view function / template rendering / filter

A, Django view function

  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.

A simple view

  Here is a view returns the current date and time in the form of an HTML document:

import datetime
def login(request):
    now=datetime.datetime.now()
    html="<p>%s</p>"%now
    return HttpResponse(html)

def login(request):
    if request.method=="GET":
        return render(request,"login.html")
    else:
        user=request.POST.get("username")
        pwd=request.POST.get("password")
        print(user,pwd)
        if user=="chao" and pwd=="123":
            return render(request,"successfully.html " )
         the else :
             return HttpResponse ( " Username Password error " ) 

Home user login and view function
Home users log on and view function

Two, FBV and CBV

FBV (function base views)  is to use the processing functions in the view request.

Copy the code
def login(request):
    if request.method=="GET":
        return render(request,"login.html")
    else:
        user=request.POST.get("username")
        pwd=request.POST.get("password")
        print(user,pwd)
        if user=="chao" and pwd=="123":
            return render(request,"successfully.html")
        else:
            return HttpResponse("用户名密码错误")
Copy the code

CBV (class base views)  is used in the view request handling.

  Python is an object-oriented programming language, if only a function to develop, there are many advantages of object-oriented missing out (inheritance, encapsulation, polymorphism). So Django later joined the Class-Based-View. It allows us to use the class to write View. The advantage of this is mainly the following two:

  1. Improve the reusability of code, object-oriented techniques may be used, such as Mixin (multiple inheritance)
  2. May be processed for various different functions HTTP method, in many if not determining, improve code readability

views view function:

Copy the code
Import django.views View from 
class MyView (View): 
    # GET request to execute a function of 
    : GET DEF (Self, Request) 
        return the render (Request, "the login.html") 
    function execution request # post 
    def post (self, request) : 
        User request.POST.get = ( "username") 
        pwd = request.POST.get ( "password") 
        Print (User, pwd) 
        IF User == "Chao" and pwd == "123": 
            return the render (Request , "successfully.html") 
        the else: 
            return HttpResponse ( "username password")
Copy the code

Each request over automatically to perform the corresponding function, what logic function execution request is what

urls search path:

from app01.views import Myview
urlpatterns = [
    url(r'^$',Myview.as_view()),
    url(r'^login/',Myview.as_view()),
]

Adding the class attribute may be provided in two ways, first Python is a common method may be overridden by a subclass.

Copy the code
from django.http import HttpResponse
from django.views import View
  
class GreetingView(View):
    name = "yuan"
    def get(self, request):
         return HttpResponse(self.name)
  
# You can override that in a subclass
  
class MorningGreetingView(GreetingView):
    name= "alex"
Copy the code

The second method, you can also specify the type of property in the url:

    Python is provided in the url attribute class

Import app01.views MyView from 
the urlpatterns = [ 
    URL (R & lt '^ $', Myview.as_view (name = "ZZZ")), the value of the name attribute specifies # 
    url (r '^ login /' , Myview.as_view ()), 
]
Import django.views View from 
class MyView (View): 
    name = "Chao" original attribute value # 
    # get request function execution 
    DEF get (Self, Request): 
        return the HttpResponse (the self.name)

The above name = "zzz" will any original category attribute value to overwrite;      inside the class must have a name attribute, and will be transferred to the incoming value of this attribute overwrite

Third, add to the view decorator

Decorator decoration FBV

    FBV is itself a function, and so add to the normal function decorators no difference:

Copy the code
def wrapper(login):
    def inner(request,*args,**kwargs):
        print("执行前")
        ret = login(request)  # login需要request这个参数,必须传
        print("执行后")
        return ret
    return inner

@wrapper
def login(request):
    if request.method=="GET":
        return render(request,"login.html")
    else:
        user=request.POST.get("username")
        pwd=request.POST.get("password")
        print(user,pwd)
        if user=="chao" and pwd=="123":
            return render(request,"successfully.html")
        else:
            return HttpResponse ( "username password")
Copy the code

To separate post request to add the functionality required to post requests individually packaged and then add a function of decorator;

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 to first convert it to a method decorators.

    Django provides method_decorator decorative function for converting a method decorator decorators.

1, decorator added directly to the corresponding HTTP request method

DEF warpper (Login):
     DEF Inner (Request, * args, ** kwargs):
         Print ( " before execution " ) 
        RET = Login (Request)
         Print ( " after performing " )
         return RET
     return Inner 

from django.views Import View
 from django.utils.decorators Import method_decorator
 class MyView (View):
     # function get request execution 
    @method_decorator (warpper)
     DEF get (Self, request):
        return render(request,"login.html")

    @method_decorator(wrapper)
    # post请求执行的函数
    def post(self,request):
        user = request.POST.get("username")
        pwd=request.POST.get("password")
        print(user,pwd)
        if user=="chao" and pwd=="123":
            return render(request,"successfully.html")
        the else :
             return HttpResponse ( " Username Password error " ) 

methods respectively add a decorator for each method
A method were added to each method decorator

To be used with caution CBV, the request will first come to perform dispatch () This method, dispatch HTTP request method is responsible for distributing the corresponding request method;

We can add the corresponding function before and after the dispatch distribution;

from django.views Import View
 class myView (View):
     DEF dispatch (Self, Request, * args, ** kwargs):
         Print ( " pre-distribution " ) 
        RET . = Super () dispatch (Request, * args, ** kwargs )
         Print ( " the distribution " )
         return RET 

    # GET request function executes 
    DEF GET (Self, request):
         return the render (request, " login.html " ) 

    # pOST request function executes 
    DEF  pOST (Self, request):
        the User= request.POST.get("username")
        pwd=request.POST.get("password")
        if user=="chao" and pwd=="123":
            return render(request,"successfully.html")
        else:
            return HttpResponse("用户名密码错误")f
Add Method Two distribution function before and after
from django.views import View
class Myview(View):
    def dispatch(self, request, *args, **kwargs):
        print("分发前")
        ret=super().dispatch(request, *args, **kwargs)
        print("分发后")
        return ret

    # get请求执行的函数
    def get(self,request):
        return render(request,"login.html")

    # post请求执行的函数
    def post(self,request):
        user = request.POST.get("username")
        pwd=request.POST.get("password")
        if user=="chao" and pwd=="123":
            return render(request,"successfully.html")
        else:
            return HttpResponse("用户名密码错误")f
方法二 分发前后添加功能

2、给dispatch添加装饰器就是给所有的Http请求添加了装饰器

def wrapper(login):
    def inner(request,*args,**kwargs):
        print("装饰器执行前")
        ret = login(request)
        print("装饰器执行后")
        return ret
    return inner

from django.views import View
from django.utils.decorators import method_decorator
class Myview(View):
    @method_decorator(wrapper)
    def dispatch(self, request, *args, **kwargs):
        ret=super().dispatch(request, *args, **kwargs)
        return ret

    # get请求执行的函数
    def get(self,request):
        return render(request,"login.html")

    # post请求执行的函数
    def post(self,request):
        user = request.POST.get("username")
        pwd=request.POST.get("password")
        if user=="chao" and pwd=="123":
            return render(request,"successfully.html")
        else:
            return HttpResponse("用户名密码错误")

方法三 给所有的HTTP请求添加装饰器
方法三 给所有的HTTP请求添加装饰器

 

3、给类添加装饰器用name属性指定给哪个HTTP请求方法添加装饰器

def wrapper(login):
    def inner(request,*args,**kwargs):
        print("装饰器执行前")
        ret = login(request)
        print("装饰器执行后")
        return ret
    return inner

from django.views import View
from django.utils.decorators import method_decorator

@method_decorator(wrapper,name="get")
class Myview(View):
    def dispatch(self, request, *args, **kwargs):
        ret=super().dispatch(request, *args, **kwargs)
        return ret

    # get请求执行的函数
    def get(self,request):
        return render(request,"login.html")

    # post请求执行的函数
    def post(self,request):
        user = request.POST.get("username")
        pwd=request.POST.get("password")
        if user=="chao" and pwd=="123":
            return render(request,"successfully.html")
        else:
            return HttpResponse("用户名密码错误")

给类添加装饰器
给类添加装饰器

四、request对象

当一个页面被请求时,Django就会创建一个包含本次请求原信息(请求报文中的请求行、首部信息、内容主体等)的HttpRequest对象。
  Django会将这个对象自动传递给响应的视图函数,一般视图函数约定俗成地使用 request 参数承接这个对象。

请求相关的常用值

  • path_info     返回用户访问url,不包括域名
  • method        请求中使用的HTTP方法的字符串表示,全大写表示。
  • GET              包含所有HTTP  GET参数的类字典对象
  • POST           包含所有HTTP POST参数的类字典对象
  • body            请求体,byte类型 request.POST的数据就是从body里面提取到的
  • META           获取请求头的元信息

例如:

官方文档

使用方法:

Copy the code
def index(request):
    print(request.method) #请求方式
    print(request.path)   #请求路径,不带参数的
    print(request.POST)   #post请求数据  字典格式
    print(request.GET)    #get的请求数据  字典格式
    print(request.META)   #请求头信息,将来用到哪个咱们再说哪个
    print(request.get_full_path())   #获取请求路径带参数的,/index/?a=1
    print(request.is_ajax())   #判断是不是ajax发送的请求,True和False
Copy the code

五、response对象

与由Django自动创建的HttpRequest对象相比,HttpResponse对象是我们的职责范围了。我们写的每个视图都需要实例化,填充和返回一个HttpResponse。

  HttpResponse类位于django.http模块中。

1、HttpResponse

import datetime
def login(request):
    if request.method=="GET":
        now = datetime.datetime.now()
        return HttpResponse(now)

2、render

import datetime
def login(request):
    if request.method=="GET":
        now = datetime.datetime.now()
        return render(request,"login.html",{"name":now})

3、redirect

给浏览器发送了一个重定向的请求,浏览器拿到你要重定向的url,然后自动发送了一个ip+端口+路径/login/,的一个请求,后端在配置对应的url('^login/',views.login)

Copy the code
import datetime
from django.shortcuts import redirect
def login(request):
    if request.method=="GET":
        now = datetime.datetime.now()
        return render(request,"login.html",{"name":now})
    else:
        user=request.POST.get("username")
        pwd=request.POST.get("password")
        print(request.path_info)
        if user=="chao" and pwd=="123":
            return redirect("/login/")   # 重定向到登录页面
        else:
            return HttpResponse("用户名密码错误")
Copy the code

点击登录按钮密码验证通过直接被重定向,路径还是登录页面;

六、摸板渲染

变量:

1
{{ 变量 }}

import datetime
def login(request):
    if request.method=="GET":
        now = datetime.datetime.now()
        return render(request,"login.html",{"name":now})  # 字典的形式传参替换相应的内容

实际上做的就是字符串替换,如果传的是对象可以通过点的方式取到对应的值

 逻辑:

1
{ %  逻辑  % }

七、过滤器

在Django的模板语言中,通过使用 过滤器 来改变变量的显示。

  过滤器的语法: {{ value|filter_name:参数 }}

  使用管道符"|"来应用过滤器。

  例如:{{ name|lower }}会将name变量应用lower过滤器之后再显示它的值。lower在这里的作用是将文本全都变成小写。

  注意事项:

  1. 过滤器支持“链式”操作。即一个过滤器的输出作为另一个过滤器的输入。
  2. 过滤器可以接受参数,例如:{{ sss|truncatewords:30 }},这将显示sss的前30个词。
  3. 过滤器参数包含空格的话,必须用引号包裹起来。比如使用逗号和空格去连接一个列表中的元素,如:{{ list|join:', ' }}
  4. '|'左右没有空格没有空格没有空格

  Django的模板语言中提供了大约六十个内置过滤器。

示例:

def login(request):
        return render(request,"login.html",{"name":"123123123123123"})
<body>

{{ name|filesizeformat }}

</body>

default

    如果一个变量是false或者为空,使用给定的默认值。 否则,使用变量的值。

{{ value|default:"nothing"}}

    如果value没有传值或者值为空的话就显示nothing

length

    返回值的长度,作用于字符串和列表。

    {{ value|length }}

    返回value的长度,如 value=['a', 'b', 'c', 'd']的话,就显示4.

filesizeformat

    将值格式化为一个 “人类可读的” 文件尺寸 (例如 '13 KB''4.1 MB''102 bytes', 等等)。例如:

{{ value|filesizeformat }}

    如果 value 是 123456789,输出将会是 117.7 MB。

slice

    切片,如果 value="hello world",还有其他可切片的数据类型

{{value|slice:"2:-1"}}

date

    格式化,如果 value=datetime.datetime.now()

{{ value|date:"Y-m-d H:i:s"}}

truncatechars

    如果字符串字符多于指定的字符数量,那么会被截断。截断的字符串将以可翻译的省略号序列(“...”)结尾。

    参数:截断的字符数

{{ value|truncatechars:9}} #注意:最后那三个省略号也是9个字符里面的,也就是这个9截断出来的是6个字符+3个省略号,有人会说,怎么展开啊,配合前端的点击事件就行啦

truncatewords

在一定数量的字后截断字符串,是截多少个单词。

    例如:‘hello girl hi baby yue ma’,

{{ value|truncatewords:3}}  #上面例子得到的结果是 'hello girl h1...'

cut

    移除value中所有的与给出的变量相同的字符串

{{ value|cut:' ' }}

    如果value为'i love you',那么将输出'iloveyou'.

join

    使用字符串连接列表,{{ list|join:', ' }},就像Python的str.join(list)

timesince(了解)

    将日期格式设为自该日期起的时间(例如,“4天,6小时”)。

    采用一个可选参数,它是一个包含用作比较点的日期的变量(不带参数,比较点为现在)。 例如,如果blog_date是表示2006年6月1日午夜的日期实例,并且comment_date是2006年6月1日08:00的日期实例,则以下将返回“8小时”:

{{ blog_date|timesince:comment_date }}

    分钟是所使用的最小单位,对于相对于比较点的未来的任何日期,将返回“0分钟”。

timeuntil(了解)

    似于timesince,除了它测量从现在开始直到给定日期或日期时间的时间。 例如,如果今天是2006年6月1日,而conference_date是保留2006年6月29日的日期实例,则{{ conference_date | timeuntil }}将返回“4周”。

    使用可选参数,它是一个包含用作比较点的日期(而不是现在)的变量。 如果from_date包含2006年6月22日,则以下内容将返回“1周”:

{{ conference_date|timeuntil:from_date }}

重点记:

safe

  Django的模板中在进行模板渲染的时候会对HTML标签和JS等语法标签进行自动转义,原因显而易见,这样是为了安全,django担心这是用户添加的数据,比如如果有人给你评论的时候写了一段js代码,这个评论一提交,js代码就执行啦,这样你是不是可以搞一些坏事儿了,写个弹窗的死循环,那浏览器还能用吗,是不是会一直弹窗啊,这叫做xss攻击,所以浏览器不让你这么搞,给你转义了。但是有的时候我们可能不希望这些HTML元素被转义,比如我们做一个内容管理系统,后台添加的文章中是经过修饰的,这些修饰可能是通过一个类似于FCKeditor编辑加注了HTML修饰符的文本,如果自动转义的话显示的就是保护HTML标签的源文件。为了在Django中关闭HTML的自动转义有两种方式,如果是一个单独的变量我们可以通过过滤器“|safe”的方式告诉Django这段代码是安全的不必转义。

def login(request):
    if request.method=="GET":
        return render(request,"login.html",{"name":"<a href='#'>点我</a>"})
<body>

{{ name }}

</body>

关闭HTML的自动转义:

<body>

{{ name|safe }}

</body>

 

Guess you like

Origin www.cnblogs.com/youxiu123/p/11600278.html