Django 04 模板標籤(if、for、url、with、autoeacape、模板繼承於引用、靜態文件加載)

 

Django 04 模板標籤(if、for、url、with、autoeacape、模板繼承於引用、靜態文件加載)css

1、if、for、url、with、autoescapehtml

urlpatterns = [
    path('hello/<name>/',views.hello),
    path('hello1', views.hello1,name='hello1'),
    path('hello2/<yy>', views.hello2,name='hello2')
]

def hello(request,name):
    return render(request,'book/index1.html',
                  context={'name':name,
                           'ls':['x','y','z'],
                           'str':'',
                           'test_test_name':'重命名的對象',
                           'html':'<h3>皮皮塔可真漂亮</h3>'

                           })

def hello1(request):
    return HttpResponse('頁面1')

def hello2(request,yy):
    return HttpResponse('頁面2: {}'.format(yy))

  if語句python

if語句、條件語句:
{% if name == 'python' %}
    這是{{ name }}的頁面
{% elif name == 'django' %}
    這是django的頁面
{% else %}
    這是錯誤的頁面
{% endif %}
<br>
循環語句:for循環
{% for i in ls %}
    {{ i }}
{% endfor %}
<br>
<br>

  for語句django

for語句:forloop系列的介紹:
{% for i in ls %}
    {% if forloop.counter == 1 %}
        <li>下標1開始 1,2,3:{{ i }}</li>
    {% endif %}
    {% if forloop.revcounter == 1 %}
        <li>下標3開始 3,2,1:{{ i }}</li>
    {% endif %}
    {% if forloop.counter0 == 1 %}
        <li>下標0開始 0,1,2:{{ i }}</li>
    {% endif %}
    {% if forloop.revcounter0 == 1 %}
        <li>下標2開始 2,1,0:{{ i }}</li>
    {% endif %}
{% endfor %}
<br>
{% for i in ls %}
    {% if forloop.first %}
        <li>第一個迭代{{ i }}</li>
    {% endif %}
{% endfor %}
<br>
{% for i in ls %}
    {% if forloop.last %}
        <li>最後一個迭代{{ i }}</li>
    {% endif %}
{% endfor %}
<br>
{% for i in ls %}
    {% for j in ls  %}
        {% if forloop.parentloop.counter == 1 %}
            {{ i }}和{{ j }}
        {% endif %}
    {% endfor %}
{% endfor %}
<br>
{% for i in str   %}
    str非空
    {% empty %}
    str空的啊
{% endfor %}
<br>
<br>

  url頁面跳轉oop

url 頁面跳轉:
<a href="/book/hello1">跳轉吧1</a> <br>
<a href="{% url 'hello1' %}">跳轉吧2</a>  <br> {# 直接跳轉name爲hello1的url #}
<a href="{% url 'hello2' 'python' %}">跳轉吧3</a> {# 跳轉name爲hello2的url,並添加變量yy爲python #}
<br>
<br>

 

   with重命名url

with:重命名 <br>
{% with test_test_name as ttn %} {# 將test_test_name重命名爲ttn #}
    {{ ttn }}
{% endwith %}
<br>
<br>

  aotoeacapespa

自動化轉義:
原始:{{ html }} <br>
過濾器:{{ html|safe }}
標籤:
{% autoescape off %}
    {{ html }}
{% endautoescape %}

 

2、模板繼承與引用code

  base.htmlorm

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>{% block title %}這是默認的題目{% endblock %}</title>
</head>
<body>
    固定的內容 <br>
{% block content %}
    這是主頁
{% endblock %}
</body>
</html>

  reference.html 子模板htm

{% extends 'book/base.html' %} {# 模板繼承base.html #}

{% block title %}           {# 修改title部分 #}
    這是reference1的頁面
{% endblock %}

{% block content %} {# 修改content部分 #}
    這是本身的頁面 <br>
    這是引用過來的: {% include 'book/include.html' %} {# 引用include.html模板 #}
{% endblock %}

  include.html 引用模板

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
<body>
    皮皮塔18年前18啦
</body>
</html>
#模板繼承總結:
#一、模板繼承使用extends標籤實現,經過block來個子模板開放接口
#二、extends必須是模板第一個出現的標籤
#三、子模板的全部內容,必須出如今父模板定義好的block中,不然django不會渲染
#四、若是出現重複代碼,就應該考慮使用引用模板
#五、儘量多的定義block,方便子模板實現更細的需求
#六、若是再某個block中,要使用父模板的內容,使用block.super獲取

 

3、靜態文件加載

#在項目目錄下建立static目錄,在該目錄下建立三個分別爲css,js,image的目錄

#在項目settings.py最後一行加上
STATICFILES_DIRS = [
    os.path.join(BASE_DIR,'static')
]
{% load static %} {# 導入static #}
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>靜態文件引用</title>
</head>
<body>
<link rel="stylesheet" href="{% static 'css/mycss.css' %}"> {# 引入css #}
<br>
<script src="{% static 'js/myjs.js' %}"></script> {# 引入js #}
<br>
<img src="{% static 'image/雙向查詢.png' %}" alt=""> {# 引入圖片 #}

</body>
</html>
相關文章
相關標籤/搜索