python-django前端传递数据的三种格式_CBV源码分析_django模板语法

11 篇文章 0 订阅

django

1. CBV 源码剖析

# 注意: 轻易不要修改源码 出了bug很难找到

# urls.py
url(r'^login/',views.MyLogin.as_view())

'''
CBV和FBV在路由匹配上的本质是一样的 都是路由对应函数内存地址

ps: 函数名/方法名 加上括号执行优先级最高

as_view() 是被@classmethod修饰的类方法
	
	@classonlymethod
    def as_view(cls, **initkwargs):
    	...
        return view
'''
# as_view函数源码
@classonlymethod
def as_view(cls, **initkwargs):
    """
    cls就是我们自己写的类
    Main entry point for a request-response process.
    """
    def view(request, *args, **kwargs):
        self = cls(**initkwargs)  # cls是我们自己写的类
        # self = MyLogin(**initkwargs)  产生一个我们自己写的类的对象
        return self.dispatch(request, *args, **kwargs)
    """
	面向对象属性方法查找顺序
        先从对象自己找
        再去产生对象的类里面找
        之后再去父类找
    	...
    总结: 看源码只要看到了self点一个东西 一定要问你自己当前这个self到底是谁
    """
    return view

# CBV 流程
1. 请求来了,路由匹配成功执行 path('index/', views.Index.as_view()),
   执行views.Index.as_view()(request)
2. 本质是执行as_view()内部有个闭包函数view(request)
3. 本质是view(request) => dispatch(request)
4. dispatch内部,根据请求的方法(get,post)=> 执行视图类中的 def get  def post
# views.py

class Index(View):
    # 允许的请求方式列表
    # http_method_names = ['post', 'get']

    def dispatch(self, request, *args, **kwargs):

        # 执行原来的dispatch
        # 类似于装饰器
        # 加代码
        # res=super().dispatch(request, *args, **kwargs)
        # # 加代码
        # return res

        # 不管什么请求来,都执行get
        return self.get(request, *args, **kwargs)

    # 没有dispatch方法,找父类,如果有会执行自己的
    def get(self, request, *args, **kwargs):
        print(request.data)
        return HttpResponse('cbv的get请求')

    def post(self, request, *args, **kwargs):
        return HttpResponse('cbv的post请求')

2. 前后端交互编码方式

1. urlencoded => 传普通的数据(form表单默认)  request.POST
2. form-data  => 传文件和数据               request.POST   request.FILES
3. json       => 传json格式数据             request.body中取出来自行处理

def index(request):
    # 接收urlencoded编码
    body体中:name=lqz&age=18
    # print(request.POST)

    # 接收form-data编码
    body体中:分两部分,一部分是数据,一部分是文件
    数据部分:name=lqz&age=18
    ---asdfasdfasdfgasgasgd---
    文件部分:(二进制)
    
    # 数据部分
    # print(request.POST)
    # 文件部分
    # print(request.FILES)

    # 接收json格式
    body体中 
    {
    "name": "lqz",
    "age": 18
	}
    # 这里没有
    print(request.POST)
    # 数据在这(自行处理)
    print(request.body)

    return HttpResponse('ok')

3. django 模板语法

1. django模板使用方式

# 模版语法重点:

  变量:{{ 变量名 }}
    
    1 深度查询 用句点符
    2 过滤器

  标签:{ %   % }
views.py
from django.template import Template,Context

def index(request):

    # 第一种方式,最常用的
    now = datetime.datetime.now()
    return render(request,'time.html',context={'current_date':str(now),'title':'lqzNB'})
    
    # 第二种方式
    now=datetime.datetime.now()
    # t = Template('<html><body>现在时刻是:<h1>{{current_date}}</h1></body></html>')
    from day65 import settings
    import os
    path=os.path.join(settings.BASE_DIR,'templates','time.html')
    print(path)
    ss=open(path,'r',encoding='utf-8').read()
    t=Template(ss)
    # t=get_template('current_datetime.html')
    c=Context({'current_date':str(now),'title':'lqzNB'})
    html=t.render(c)  # html是渲染后的字符串
    # 本质就是render(request,'home.html',context={'current_date':str(now)})
    return HttpResponse(html)
time.html
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>{{ title }}</title>
</head>
<body>
现在时刻是:<h1>{{ current_date }}</h1>
</body>
</html>

2. 模板语法之变量

DTL: Django Template Language

在 Django 模板中遍历复杂数据结构的关键是句点字符, 语法:{{变量名}}

views.py
模板中使用 {{ python变量 }}

def index(request):
    num = 10
    ss = 'lqz is handsome'
    b = False
    ll = [1, 2, 43]
    dic = {'name': 'lqz', 'age': 18}

    def test():
        print('我是test')
        return 'test ---撒发射点'

    class Person():
        def __init__(self, name):
            self.name = name

        def print_name(self):
            return self.name
        def __str__(self):
            return self.name

    p=Person('lqz')

    # return render(request, 'index.html',{'num':num,'ss':ss,'b':b})
    # locals() 把当前作用域下所有的变量,都传到context中
    return render(request, 'index.html',locals())
index.html
# index.html
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>{{ss}}</title>
</head>
<body>

<h1>模板语法之变量</h1>

<p>数字:{{ num }}</p>
<p>字符串:{{ ss }}</p>
<p>布尔:{{ b }}</p>
<p>列表:{{ ll }}</p>
<p>字典:{{ dic }}</p>
<p>函数:{{ test }}</p>
<p>对象:{{ p }}</p>

</body>
</html>

3. 模板语法之深度查询句点符

views.py
def index(request):
    num = 10
    ss = 'lqz is handsome'
    b = False
    ll = [1, 2, 43, {'name': 'egon'}]
    dic = {'name': 'lqz', 'age': 18}

    def test():
        print('我是test')
        return 'test ---撒发射点'

    class Person():
        def __init__(self, name):
            self.name = name

        def print_name(self):
            return self.name

        def __str__(self):
            return self.name

    p = Person('lqz')

    link1 = '<a href="https://www.baidu.com">点我<a>'
    link2 = mark_safe(link1)

    input_1='<p>用户名:<input type="text" name="name"></p>'
    input_2=mark_safe(input_1)

    script_1='''
        <script>
        alert('你被攻击了')
        </script>
    '''
    script_2 =mark_safe(script_1)
    return render(request, 'index.html', locals())
index.html
<h2>模板语法之句点符的深度查询</h2>
<p>列表的第一个元素:{{ ll.0 }}</p>
<p>字典的name对应的值:{{ dic.name }}</p>
<p>列表的第三个元素的name对应的值:{{ ll.2.name }}</p>
<p>函数执行,直接写函数名即可:{{ test }}</p>
<p>函数如果有参数?不支持</p>
<p>对象调用方法: {{ p.print_name }}</p>
<p>对象调用属性: {{ p.name }}</p>
<hr>
<a href="https://www.baidu.com">点我</a>
<p>a标签的字符串: {{ link1 }}</p>
<p>a标签的字符串,显示成a标签: {{ link2 }}</p>

<p>用户名:<input type="text" name="name"></p>
<p>input标签:{{ input_1 }}</p>
<p>input标签,显示成标签:{{ input_2 }}</p>

<p>js原封不动显示:{{ script_1 }}</p>
{{ script_2 }}

# 补充
from django.utils.safestring import mark_safe

link1 = '<a href="https://www.baidu.com">点我<a>'
link2 = mark_safe(link1)

{link1|safe}

4. 模板过滤器

default,length,filesizeformat,date,slice,truncatechars,safe

index.html
1. {{ 参数1|过滤器名字: 参数2 }}
2. 过滤器最多传两个值,最少一个值  {{ 'lqz is'|slice: '2:3' }}

<h1>过滤器</h1>

<p>过滤器之default:{{ num|default:'没有值' }}</p>
<p>过滤器之length:{{ ll|length }}</p>
<p>过滤器之length:{{ dic|length }}</p>
<p>过滤器之length:{{ 'das'|length }}</p>

{#  模板语法的注释  #}

# 了解
<p>过滤器之filesizeformat:{{ num|filesizeformat }}</p>

# lqz is handsome
<p>过滤器之slice:{{ ss|slice:"7:11" }}</p>

# 第二个参数写0,1,2, 都是 ...,  最少从3开始
<p>过滤器之truncatechars:{{ ss|truncatechars:'30' }}</p>
<p>truncatewords:{{ ss|truncatewords:'2' }}</p>

# 记住
<p>过滤器之date:{{ ctime|date:'Y年m月d日-------H时i分s秒' }}</p>
<p>过滤器之safe:{{ link1|safe }}</p>

5. 模板标签

for,for … empty,if,with,csrf_token

标签语法: { % tag % }

标签比变量更加复杂:

​ 一些在输出中创建文本,一些通过循环或逻辑来控制流程,一些加载其后的变量将使用到的额外信息到模版中。一些标签需要开始和结束标签 (例如 {% tag %} … 标签 内容 … {% endtag %})

for标签

遍历每一个元素:

{% for person in person_list %}
    <p>{{ person.name }}</p>
{% endfor %}

可以利用{% for obj in list reversed %}反向完成循环

遍历一个字典:

{% for key,val in dic.items %}
    <p>{{ key }}:{{ val }}</p>
{% endfor %}

注:循环序号可以通过{{ forloop }}显示

forloop.counter            
	The current iteration of the loop (1-indexed) 当前循环的索引值(从1开始)
	
forloop.counter0           
	The current iteration of the loop (0-indexed) 当前循环的索引值(从0开始)
	
forloop.revcounter         
	The number of iterations from the end of the loop (1-indexed) 当前循环的倒序索引值(从1开始)
	
forloop.revcounter0        
	The number of iterations from the end of the loop (0-indexed) 当前循环的倒序索引值(从0开始)
	
forloop.first              
	True if this is the first time through the loop 当前循环是不是第一次循环(布尔值)
	
forloop.last               
	True if this is the last time through the loop 当前循环是不是最后一次循环(布尔值)
	
forloop.parentloop         
	本层循环的外层循环
for … empty

for标签带有一个可选的{% empty %}从句,以便在给出的组是空的或者没有被找到时,可以有所操作。

{% for person in person_list %}
    <p>{{ person.name }}</p>

{% empty %}
    <p>sorry,no person here</p>
{% endfor %}
if 标签

{% if %} 会对一个变量求值,如果它的值是“True”(存在、不为空、且不是boolean类型的false值),对应的内容块会输出。

{% if num > 100 or num < 0 %}
    <p>无效</p>
{% elif num > 80 and num < 100 %}
    <p>优秀</p>
{% else %}
    <p>凑活吧</p>
{% endif %}

if 语句支持 and 、or、==、>、<、!=、<=、>=、in、not in、is、is not判断。

with标签

使用一个简单地名字缓存一个复杂的变量

当你需要使用一个“昂贵的”方法(比如访问数据库)很多次的时候是非常有用的

{% with total=business.employees.count %}
    {{ total }} employee{{ total|pluralize }}
{% endwith %}
csrf_token标签

{% csrf_token%}

这个标签用于跨站请求伪造保护

views.py
# views.py
def index(request):
    ll=['lqz','egon','zs','ls','ww']
    # ll=[]
    dic={'name':'lqz','age':19}
    count=1

    lqzisnbplus='lqz'
    # b=False
    b=True
    user_list=[{'name':'lqz','age':19},{'name':'egon','age':18},{'name':'张三','age':22},{'name':'李四','age':99},{'name':'asdfasdf','age':18},{'name':'暗室逢灯n','age':18}]
    return render(request, 'index.html', locals())
index.html
<h1>模板语法之标签</h1>

<h2>for的用法</h2>
{% for l in ll %}
    {# <p>{{ l }}</p> #}
    <p><a href="http://127.0.0.1:8080/{{ l }}">{{ l }}</a></p>
{% endfor %}

<hr>
{% for k,v in dic.items %}
    <p>key值为:{{ k }},value值为{{ v }}</p>
{% endfor %}

<table border="1">
    <tr>
        <td>id号</td>
        <td>用户名</td>
        <td>年龄</td>
    </tr>
    {% for dic in user_list %}
        <tr>
            <td>{{ forloop.counter }}</td>
            <td>{{ dic.name }}</td>
            <td>{{ dic.age }}</td>
        </tr>
    {% endfor %}
</table>

<h2>for...empty的用法</h2>
<ul>
    {% for l in ll %}
        <li>{{ l }}</li>
    {% empty %}
        <li>没有数据</li>
    {% endfor %}
</ul>

<h2>forloop对象</h2>
{% for dic in user_list %}
    {% for key,value in dic.items %}
        {{ forloop.parentloop.counter }}
        <p>{{ key }}:{{ value }}</p>
    {% endfor %}
{% endfor %}

<h2>if</h2>
{% if b %}
    <p>b是true的</p>
{% else %}
    <p>b是false的</p>
{% endif %}

<h2>with重命名</h2>
{% with forloop.parentloop.counter as aaa %}
    {{ aaa }}
{% endwith %}

{% with lqzisnbplus as a %}
{{ a }}
{{ lqzisnbplus }}
{% endwith %}

<h2>csrf</h2>
{% csrf_token %}
<input type="text" name="csrfmiddlewaretoken" value="uC35XuP1J2Va74ArYiNw4TMZ0PaZ6V4qvVGCsUQcqiKF5Sr8IrWS0rzpmOmPBrjY">
</body>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

I believe I can fly~

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值