django of the view layer and template layer 04

Route: The view function memory address

View layer

Template and render method is a combination of two objects used Contex

from django .template import Template,Context
def func1(request):
    res = Template('<h1>{{user}}</h>')
    con=Context({'user':{'username':'zhang','password':123,}})
    return HttpResponse(res.render(con))

JsonResponse objects

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

# ensure_ascii=False 默认等于True,设置成False,遇到中文不再进行转码,仅仅只是进行序列化,转换成json形式的字符串
import json
def func(request):
    d={'a':1,'b':3,'c':3,'d':4,'e':'速度发货!!!!!!!'}
    return HttpResponse(json.dumps(d,ensure_ascii=False),)  # 
from django.http import JsonResponse
def func(request):
    d={'a':1,'b':3,'c':3,'d':4,'e':'速度发货!!!!!!!'}
    l=[1,2,3,4,5,]
   # return JsonResponse(d)  # 加括号是个对象,内部也是基于json.dumps进行的序列化,默认遇到中文也会转成ASCII编码,
    # return JsonResponse(d,json_dumps_params={'ensure_ascii':False})  # 遇到中文不转码
    return JsonResponse(l,safe=False)  # In order to allow non-dict objects to be serialized set the safe parameter to False.  根据报错信息,去源码找到JsonResponse不能序列化其他数据类型的原因,就是修改safe=True 为False,之后就能序列化其他数据类型(json模块能序列化的)了

CBV and source code analysis

We've written before are based on the view function, called FBV. The view can also be written as class-based.
FBV: based on the view function
CBV: view class-based

from django.views import View

class MyLogin(View):
    def get(self,request):
        print('我是MyLogin里面的get方法')
        return render(request,'login.html')

    def post(self,request):
        print('我是MyLogin里面的post方法')
        return HttpResponse('post')

    # 路由的书写 与CBV有点不同
    # FBV写法     路由 >>> 视图函数内存地址
    url(r'^index/',views.index),
    # CBV写法
    url(r'^login/',views.MyLogin.as_view())  # views.view  本质也是FBV
     http_method_names = ['get', 'post', 'put', 'patch', 'delete', 'head', 'options', 'trace']
    
     def http_method_not_allowed(self, request, *args, **kwargs):
        logger.warning(
            'Method Not Allowed (%s): %s', request.method, request.path,
            extra={'status_code': 405, 'request': request}
        )
        return http.HttpResponseNotAllowed(self._allowed_methods())
    
    @classonlymethod
    def as_view(cls, **initkwargs):
        def view(request, *args, **kwargs):  # 闭包函数
            self = cls(**initkwargs)  # cls是我们自己写的类 MyLogin  self是我们自己定义的类的对象
            # 在看源码的时候 你一定要把握住一个顺序 对象在查找属性和方法的时候
            # 先从对象自身找 再去产生对象的类中找 再去类的父类中找
            return self.dispatch(request, *args, **kwargs)
        return view


    def dispatch(self, request, *args, **kwargs):
        # Try to dispatch to the right method; if a method doesn't exist,
        # defer to the error handler. Also defer to the error handler if the
        # request method isn't on the approved list.
        # 判断当前请求方式在不在默认的八个方法内
        # 1.先以GET请求为例
        if request.method.lower() in self.http_method_names:
            # 利用反射去我们自己定义类的对象中查找get属性或者是方法  getattr(obj,'get')
            # handler = get方法
            handler = getattr(self, request.method.lower(), self.http_method_not_allowed)
            else:
                handler = self.http_method_not_allowed
                return handler(request, *args, **kwargs)  # 调用get方法

CBV plus decorator way

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

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


# FBV版添加班级
@wrapper
def add_class(request):
    if request.method == "POST":
        class_name = request.POST.get("class_name")
        models.Classes.objects.create(name=class_name)
        return redirect("/class_list/")
    return render(request, "add_class.html")

CBV plus decorator way

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

Should pay attention to # use CBV, the request came first performs dispatch () This method, if required quantities of a specific request processing methods, such as get, post, and so do some of the operations time, where we can manually rewrite the dispatch method, the dispatch methods to increase and decorators in the FBV the same effect.

给CBV加装饰器 推荐你使用内置模块
from django.utils.decorators import method_decorator

# 2.可以指定给谁装
# @method_decorator(wrapper,name='post')  # name=... 表示指定给谁装
# @method_decorator(wrapper,name='dispatch')
class MyLogin(View):
    @method_decorator(wrapper)
    def dispatch(self, request, *args, **kwargs):  # 如果你想在视图函数执行之前 做一些操作 你可以在你的CBV中定义dispatch方法来拦截

        return super().dispatch(request,*args,**kwargs)
    # @outter  # 1.直接写
    # @method_decorator(outter)  # 1.推荐写法
    def get(self,request):
        print('我是MyLogin里面的get方法')
        return render(request,'login.html')
    # @outter
    def post(self,request):
        print('我是MyLogin里面的post方法')
        time.sleep(1)
        return HttpResponse('post')

Template layer

Template syntax by value

Template syntax

Writing only two formats:
{} {} related variables
{} Logical associated %%

Template by value: python all the basic data types supported by value

variable

Click Django template language syntax used in: {{name}} variable.

When the template engine encounters a variable, it will calculate the variable, and then replace it with the result itself. Named variables include any alphanumeric and underscore ( "_") combination. Variable names can not contain spaces or punctuation.

Dot (.) Has a special meaning in the template language. When the template system encounters a dot ( "."), It will be in this order inquiries:

Dictionary lookup (Dictionary lookup)
property or method query (Attribute or method lookup)
digital index query (Numeric index lookup)

Precautions:

  1. If the value of calculation result is called, it will be invoked with no parameters. Result of the call will be the value of the template.
  2. If variables does not exist, the system will insert the value string_if_invalid template option, which is the default setting is '' (the empty string)
# test.py文件
def test(request):
    n=1
    f=1.11
    s='hello baby~'
    l=[1,2,3,4,]
    t=(1,2,3,44,55)
    d={'name':'zhangsan','hobby':'read'}
    se={12,13,14,15,}
    b=False
    def index1():  #给HTML页面传递函数名的时候 模板语法会自动加括号调用该函数 并且将函数的返回值当做展示依据,模板语法不支持函数传参 也就意味着 你传给html页面的只能是不需要传参调用的函数
        return '都是废话'
    class Test(object):
        def get_self(self):
            return 'get_self'
    obj = Test()
    return render(request,'test.html',locals())

# 传类名像函数名一样,也是加括号调用,实例化一个对象;传对象,就是对象本身,是类的对象的内存地址;只要是能够加括号调用的 传递到html页面上都会自动加括号调用
<!--test.html文件-->
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <script src="https://cdn.bootcss.com/jquery/3.4.1/jquery.min.js"></script>
    <link rel="stylesheet" href="https://cdn.bootcss.com/twitter-bootstrap/3.4.1/css/bootstrap.min.css">
    <script src="https://cdn.bootcss.com/twitter-bootstrap/3.4.1/js/bootstrap.min.js"></script>

</head>
<body>
<p>{{ n }}</p>
<p>{{ f }}</p>
<p>{{ s }}</p>
<p>{{ l }}</p>
<p>{{ se }}</p>
<p>{{ d }}</p>
<p>{{ b }}</p>
<p>传函数名:{{ index1 }}</p>
<p>传函数名:{{ Test }}</p>   
<p>传函数名:{{ obj }}</p>  
</body>
</html>

Filter (the Filters)

Template syntax also give you some built-in method to help you quickly process the data
filter syntax: {{value | filter_name: parameters}}

For example: {{name | lower}} then the variable name will show its value after the application of lower filter. lower in action here is all lowercase text.

Precautions:

1. The filter supports "chain" operation. I.e., a filter output as input to another filter.
2. The filter can accept parameters, for example: {{sss | truncatewords: 30 }}, which will display the first 30 words of sss.
3. The filter parameter contains a space, it must be wrapped in quotes. Such as comma and a space used to connect the elements of a list, such as: {{List | the Join: ','}}
4. '|' no space left no space is no space

Front and rear end unescaping
the front
| safe
back-end
from django.utils.safestring Import mark_safe
SSS2 = "

My h2 tag

"
res = mark_safe(sss2)

<p>统计长度(如果无法统计默认返回0):{{ s|length }}</p>
<p>加法运算(内部异常捕获 支持数字相加 字符串拼接 都不符合返回空):{{ n|add:f }}</p>
<p>切片操作 顾头不顾尾 也支持步长:{{ l|slice:'0:5:2' }}</p>
<p>判断是否有值(有值展示值本身 没值展示默认值):{{ is_value|default:'is_value变量名指向的值为空' }}</p>
<p>自动转成文件大小格式:{{ file_size|filesizeformat }}</p>
<p>截取文本内容(字符) 截取五个字符 三个点也算:{{ s|truncatechars:8 }}</p>
<p>截取文本内容(按照空格计算) 截取五个字符 三个点不算 :{{ s1|truncatewords:5 }}</p>
<p>默认情况下,不会转换成前端html标签 :{{sss}}</p>  # 后端传来的字符串类型的标签
<p>展示带有标签的文本:{{ sss|safe }}</p> 添加safe参数之后可以转换后端传来的字符串标签
<p>展示带有标签的文本:{{ ss|safe }}</p>  后端: ss='<script>alert(123)</script>'

label

Related tag logic
IF
for loop

{% for foo in l %}
    {{ forloop }}
    {% if forloop.first %}
        <p>{{ foo }}</p>
​   {% elif forloop.last %}
​   {% else %}
        <p>{{ foo }}</p>
​   {% endif %}
​   {% empty %}
        <p>档for循环的对象为空的时候走这个!</p>
{% endfor %}

The value of template syntax uses only one way to unify a period character (.)

<p>{{ comp_dic.hobby.2.2.age }}</p>
<p>当你的数据是通过比较复杂的点点点获取到的后续又需要经常使用 你可以给该数据起别名 别名只能在with内部使用</p>
{% with comp_dic.hobby.2.2.age as age  %}    
    <p>{{ age }}</p>
    <p>{{ comp_dic.hobby.2.2.age }}</p>
{% endwith %}

Custom filters and labels

Custom filter

django supports user-defined
must first have three ready
1. In an application under the name of a new name must be called templatetags folder
2. Create an arbitrary name in the folder py file
3. must be written in the py file The following two codes
from the django.template Import Library
Register = Library ()

Then you can use the register customize filters and labels

# 应用名下templatetag文件夹 mytag.py文件
from django.template import Library
register = Library()
# 自定义过滤器,跟默认的过滤器一样,最多只能接受两个参数
@register.filter(name='xxx') # 给过滤器起名字
def index(a,b):
    return a+b

Use custom filters need to be loaded on the html page.

<!--test.html文件-->
{% load mytag %}
{{ 1|xxx:99 }}

Custom label

# 应用名下templatetag文件夹 mytag.py文件

# 自定义标签   可以接受任意多个参数
@register.simple_tag(name='zzz')
def mytag(a,b,c,d):
    return '%s?%s?%s?%s'%(a,b,c,d)
<!--test.html文件-->
{% load mytag %}
{% zzz 'a' 'b' 'c' 'd' %}
<!--test.html文件-->
<p>自定义的过滤器可以在逻辑语句使用 而自定义的标签不可以</p>
{% if 1|xxx:99 %}
    <p>有值</p>
{% else %}
    <p>无值</p>
{% endif %}

Custom inclusion_tag

Is a function of the outside world can accept incoming parameters, and then passed to a html page, after obtaining the data rendered, will render the page into a good place to call inclusion_tag page

# 应用名下templatetag文件夹 mytag.py文件
# 自定义inclusion_tag
@register.inclusion_tag('mytag.html',name='xxx')
def index666(n):
    l = []
    for i in range(n):
        l.append('第%s项'%i)
    return locals()  # 将l直接传递给mytag.html页面
<!--mytag.html文件-->
<!--只做临时渲染的页面,所以该页面的其他框架部分html代码就不需要了-->
<ul>
    {% for foo in l %}
        <li>{{ foo }}</li>
    {% endfor %}
</ul>
<!--test.html文件-->
<p>自定义inclusion_tag的使用  当你需要使用一些页面组件的时候 并且该页面组件需要参数才能够正常渲染 你可以考虑使用inclusion_tag</p>
{% load mytag %}
{% xxx 5 %}

Inherited template

You need to pre-defined area on the page that you want to use, then in succession, you can use your designated area, which means, if you do not designate any area, then you will not be able to modify the page content.

<!--先在页面上利用block划定你以后可能想改的区域-->
{% block content %}
{% endblock %}

<!--继承之后  就可以通过名字找到对应的区域进行修改-->
{% extends 'home.html' %}

{% block content %}
<!--修改模板中content区域内容-->
{% endblock %}
            
模板上的block区域越多 页面的扩展性越强
建议你一个模板页面至少有三块区域:
    css区域
    html代码区域  可以设置多个block
    js区域
有了这三块区域 就能够实现每一个页面都有自己独立的css和js代码

{% extends 'home.html' %}  子文件就可以通过这个方法继承猪文件的html代码的格式
{% block css %}
<style>
    p {
        color: green;
    }
</style>
{% endblock %}

{% block content %}
<p>login页面</p>
{% endblock %}

{% block js %}
<script>
    alert('login')
</script>
{% endblock %}

你还可以在子页面上继续沿用父页面的内容
        {{ block.super }}

Inherited template
1. On the first page you want to inherit through the block demarcated area you might want to change the future of
2. The first sub-inheritance extends on page
3. Use block automatically prompted to select the area you want to modify content

Importing templates

Html page as the module is directly introduced using
{% include 'bform.html'%}

Guess you like

Origin www.cnblogs.com/zhangchaocoming/p/11939438.html