jinja2 模æ¿è¯è¨ä¹filter 3
æ¯æ¿
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="x-ua-compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Title</title>
{% block page-css %}
{% endblock %}
</head>
<body>
<h1>è¿æ¯æ¯æ¿çæ é¢</h1>
{% block page-main %}
{% endblock %}
<h1>æ¯æ¿åºé¨å
容</h1>
{% block page-js %}
{% endblock %}
</body>
</html>
注æï¼æ们é常ä¼å¨æ¯æ¿ä¸å®ä¹é¡µé¢ä¸ç¨çCSSååJSåï¼æ¹ä¾¿å页é¢æ¿æ¢ã
继æ¿æ¯æ¿
å¨å页é¢ä¸å¨é¡µé¢æä¸æ¹ä½¿ç¨ä¸é¢çè¯æ³æ¥ç»§æ¿æ¯æ¿ã
{% extends 'base.html' %}
åï¼blockï¼
éè¿å¨æ¯æ¿ä¸ä½¿ç¨{% block xxx %}
æ¥å®ä¹"å"ã
å¨å页é¢ä¸éè¿å®ä¹æ¯æ¿ä¸çblockåæ¥å¯¹åºæ¿æ¢æ¯æ¿ä¸ç¸åºçå 容ã
{% block page-main %}
<p>ä¸æ
è</p>
<p>人æ
æ¶</p>
<p>é¨éé»æè±æè½</p>
{% endblock %}
ç»ä»¶
å¯ä»¥å°å¸¸ç¨ç页é¢å 容å¦å¯¼èªæ¡ï¼é¡µå°¾ä¿¡æ¯çç»ä»¶ä¿åå¨åç¬çæ件ä¸ï¼ç¶åå¨éè¦ä½¿ç¨çå°æ¹æå¦ä¸è¯æ³å¯¼å ¥å³å¯ã
{% include 'navbar.html' %}
éææ件ç¸å ³
{% load static %}
<img src="{% static "images/hi.jpg" %}" alt="Hi!" />
å¼ç¨JSæ件æ¶ä½¿ç¨ï¼
{% load static %}
<script src="{% static "mytest.js" %}"></script>
æ个æ件å¤å¤è¢«ç¨å°å¯ä»¥å为ä¸ä¸ªåéï¼
{% load static %}
{% static "images/hi.jpg" as myphoto %}
<img src="{{ myphoto }}"></img>
使ç¨get_static_prefix
{% load static %}
<img src="{% get_static_prefix %}images/hi.jpg" alt="Hi!" />
æè
{% load static %}
{% get_static_prefix as STATIC_PREFIX %}
<img src="{{ STATIC_PREFIX }}images/hi.jpg" alt="Hi!" />
<img src="{{ STATIC_PREFIX }}images/hi2.jpg" alt="Hello!" />
èªå®ä¹simpletag
åèªå®ä¹filter类似ï¼åªä¸è¿æ¥æ¶æ´çµæ´»çåæ°ã
å®ä¹æ³¨åsimple tag
@register.simple_tag(name="plus")
def plus(a, b, c):
return "{} + {} + {}".format(a, b, c)
使ç¨èªå®ä¹simple tag
{% load app01_demo %}
{# simple tag #}
{% plus "1" "2" "abc" %}
inclusion_tag
å¤ç¨äºè¿åhtml代ç ç段
示ä¾ï¼
templatetags/my_inclusion.py
from django import template
register = template.Library()
@register.inclusion_tag('result.html')
def show_results(n):
n = 1 if n < 1 else int(n)
data = ["第{}项".format(i) for i in range(1, n+1)]
return {"data": data}
templates/snippets/result.html
<ul>
{% for choice in data %}
<li>{{ choice }}</li>
{% endfor %}
</ul>
templates/index.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="x-ua-compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>inclusion_tag test</title>
</head>
<body>
{% load inclusion_tag_test %}
{% show_results 10 %}
</body>
</html>
上一篇: python tkinter做的生成计算题的GUI
下一篇: python通过cython加密代码
推荐阅读
-
python将一组数分成每3个一组的实例
-
python3 实现一行输入,空格隔开的示例
-
3DMax中怎么设置V-ray渲染器通用参数?
-
小米发布3款4K电视 成印度智能电视*
-
pow函数(数学次方)在c语言的用法,两种编写方法实例( 计算1/1-1/2+1/3-1/4+1/5 …… + 1/99 - 1/100 的值)
-
小米全面屏电视Pro共3款机型:全系2+32大存储
-
PHP getID3类的使用方法学习笔记【附getID3源码下载】
-
小米平板电脑能定位吗?3种可实现小米平板定位导航的方法介绍
-
锤子、一加手机这些3C大牌厂商缘何纷纷联手京东跨界时尚?
-
Python3使用pandas模块读写excel操作示例