django的模版


django的模版

1模版语法

 views.py 书写方式

def index(request):

    '''
    模版语法分:

    变量: {{ }}
        1. 深度查询   句点符
        2. 过滤器     {{val|filter_name:参数}}

    标签: {%  %}

    '''

    name = "augustyang"
    test_list = [11,22,33,44]

    class people:
        def __init__(self,name,age):
            self.name = name
            self.age = age

    p1 = people("yy",18)
    p2 = people("ss",28)

    people_list = [p1,p2]

    # return render(request, "index.html",{"name":name})
    return render(request, "index.html",locals())
'''
用到的知识有:
    引用方式:
        {"name":name} 指定引用
        locals()    相当于全部引用  
          
            render(request, "index.html",{"name":name,"p1":p1})
        
    index.html 文件 调用方式
    
        1. 深度查询   句点符
        
        

{{ name }}

{{ p1 }}

{{ p1.name }}

{{ test_list.1 }}

{{ people_list.1.name }}

'''

   index.html 书写方式

        <p> {{ name }}p>
        <p> {{ p1 }}p>
        <p> {{ p1.name }}p>
        <p> {{ test_list.1 }}p>
        <p> {{ people_list.1.name }}p>

2过滤器

 views.py 书写方式

def index(request):
  
    ysl = 22# 过滤器
    import datetime
    now = datetime.datetime.now()
    file_size = 12343242123123

    content = "南京出台人才购房新政,全市所有在售房源向海内外人才全开放,并为人才购房开辟优先通道,该政策将于2020年1月1日起施行。据中新经纬客户端不完全统计"

    link = "click"

    return render(request, "index.html",locals())

  index.html 书写方式

    
    <h2>过滤器h2>

    
    <p>{{ now|date:"Y-m-d " }}p> 

    
    <p>{{ test_list|length }}p> 

    
    <p>{{ file_size|filesizeformat }}p>

    
    <p>{{ ysl|default:"没有这个值" }}p>

    
    <p>{{ link|safe }}p>

    
    <p>{{ content|truncatechars:10 }}p>

3标签

 标签 有for循环    if 循环  with as 重命名

①for标签

{% for foo in test_list %}
    <p>{{ foo }}p>
{% endfor  test_list%}

{% for foo in people_list %}
    <p>{{ foo.name }}p>
{% endfor   people_list %}



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


{% for key in dic %}
    <p>{{ key }}p>
{% endfor %}



{% for people in people_list %}
    <p>{{ people }}p>
{% endfor %}

 ②if 标签

{% if user %}
    <p>
        <a href="">hi {{ user }}a>
        <a href="">注销a>
    p>
{% else %}
    <p>
        <a href=" ">登录a>
        <a href=" ">注册a>
    p>

{% endif %}

③with标签

{% with people_list.1.name as n  %}
<p>{{ n }}p>
<p>{{ n }}p>
{% endwith %}

④ csrf_token

在form表单点击提交按钮后,会出现Forbidden:CSRF verification failed. Request aborted,解决方法两种

方法一:在settings.py中注释掉 'django.middleware.csrf.CsrfViewMiddleware',

方法二:在form表单中 添加这个模板语言

DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Titletitle>
head>
<body>
    <form method="post" action="">
       name: <input type="text" name="user" >
            <input type="submit" name="">
         {% csrf_token %}
    form>
body>
html>

4自定义标签和过滤器

前提

1 在APP中新建一个包 templatetags 必须是这个名字

2 新建一个py文件 my_tag_filter.py

3.在my_tag_filter.py 文件中

from django import template
register=template.Library()


@register.filter
def multi_fliter(x,y):

    return x*y


@register.simple_tag
def multi_tag(x,y):

    return x*y

 2. 使用

<h2>自定义标签和过滤器h2>
{% load my_tag_filter %}


<p>{{ ysl|multi_fliter:20 }}p>

<p>{% multi_tag 7 8 %}p>

{% if ysl|multi_fliter:22 > 100 %}
    <p>100p>
{% else %}
    <p>{{ ysl }}p>
{% endif %}

{#{% if  multi_tag ysl 20 >100 %}#}
{#<p>100p>#}
{#{% else %}#}
{#<p>{{ ysl }}p>#}
{#{% endif %}#}

 需要先引用

{% load my_tag_filter %}
filter可以用在if等语句,simple_tag不可以 

5模版继承

1引用bootstrap,jquery

①在工程下新建一个包  一般叫static   

②配置路径

# static 是静态文件的别名
STATIC_URL = '/static/'


# 配置 静态文件
STATICFILES_DIRS=[
    os.path.join(BASE_DIR,"static"),
]

③重启 django 加载配置

"stylesheet" type="text/css" href="/static/bootstrap-3.3.7/css/bootstrap.css">

2. 模版继承样例

advertise.html

<div class="panel panel-danger">
    <div class="panel-heading">Panel heading without titlediv>
    <div class="panel-body">
        11111
    div>
div>

<div class="panel panel-success">
    <div class="panel-heading">Panel heading without titlediv>
    <div class="panel-body">
        3333
    div>
div>

<div class="panel panel-info">
    <div class="panel-heading">Panel heading without titlediv>
    <div class="panel-body">
        555555
    div>
div>

base.html

DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">

    
    {% block title %}
        <title>basetitle>
    {% endblock %}

    <link rel="stylesheet" type="text/css" href="/static/bootstrap-3.3.7/css/bootstrap.css">
    <style>

        .header {
            width: 100%;
            height: 50px;
            background-color: #369;
        }
    style>
    <script type="text/javascript" src="/static/jquery-3.4.1.js">script>

head>
<body>
<div class="header">div>
<div class="container">
    <div class="row" style="margin-top: 50px;">
        <div class="col-lg-4  col-mg-4">

            
            {% include 'advertise.html' %}


        div>
        <div class="col-lg-8  col-mg-8">

            
            {% block con %}
                <h4>contenth4>
            {% endblock %}

        div>
    div>
div>
body>
html>

auth.html

{% extends 'base.html' %}


{% block title %}
<title>authtitle>
{% endblock %}


{% block con %}


{#{{ block.super }}#}

<h4>loginh4>
<h4>loginh4>

{% endblock con%}

index.html

{% extends "base.html" %}

{% block title %}
<title>indextitle>
{% endblock %}


{% block con %}

{#    <p> {{ name }}p>#}
{#    <p> {{ p1 }}p>#}
{#    <p> {{ p1.name }}p>#}
{#    <p> {{ test_list.1 }}p>#}
{#    <p> {{ people_list.1.name }}p>#}


    
    <h2>过滤器h2>

    
    <p>{{ now|date:"Y-m-d " }}p> 

    
    <p>{{ test_list|length }}p> 

    
    <p>{{ file_size|filesizeformat }}p>

    
    <p>{{ ysl|default:"没有这个值" }}p>

    
    <p>{{ link|safe }}p>

    
    <p>{{ content|truncatechars:10 }}p>



<h2>标签h2>

{% for foo in test_list %}
    <p>{{ foo }}p>
{% endfor  test_list%}

{% for foo in people_list %}
    <p>{{ foo.name }}p>
{% endfor   people_list %}



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


{% for key in dic %}
    <p>{{ key }}p>
{% endfor %}



{% for people in people_list %}
    <p>{{ people }}p>
{% endfor %}


<hr>

{% if user %}
    <p>
        <a href="">hi {{ user }}a>
        <a href="">注销a>
    p>
{% else %}
    <p>
        <a href=" ">登录a>
        <a href=" ">注册a>
    p>

{% endif %}

{% with people_list.1.name as n  %}
<p>{{ n }}p>
<p>{{ n }}p>
{% endwith %}

<hr>
<h2>自定义标签和过滤器h2>
{% load my_tag_filter %}


<p>{{ ysl|multi_fliter:20 }}p>

<p>{% multi_tag 7 8 %}p>

{% if ysl|multi_fliter:22 > 100 %}
    <p>100p>
{% else %}
    <p>{{ ysl }}p>
{% endif %}

{#{% if  multi_tag ysl 20 >100 %}#}
{#<p>100p>#}
{#{% else %}#}
{#<p>{{ ysl }}p>#}
{#{% endif %}#}



{% endblock %}

需要注意项:

1  如果你在模版中使用 {% extends %} 标签,它必须是模版中的第一个标签。其他的任何情况下,模版继承都将无法工作。

2  在base模版中设置越多的 {% block %} 标签越好。请记住,子模版不必定义全部父模版中的blocks,所以,你可以在大多数blocks中填充合理的默认内容,然后,只定义你需要的那一个。多一点钩子总比少一点好。

3   如果你发现你自己在大量的模版中复制内容,那可能意味着你应该把内容移动到父模版中的一个 {% block %} 中。

4   为了更好的可读性,你也可以给你的 {% endblock %} 标签一个 名字 。例如:

{% block content %}

...

{% endblock content %}