海上月明

          editer by sun
          posts - 162, comments - 51, trackbacks - 0, articles - 8
             :: 首頁 :: 新隨筆 ::  :: 聚合  :: 管理

          DjangoBook note

          Posted on 2006-11-28 20:27 pts 閱讀(268) 評論(0)  編輯  收藏 所屬分類: Django

          DjangoBook note
          模板
          1、用 html 文件保存,設計的變量用 {{value_name}} 填充
          2、需 from django.template import Template,Context 導入庫
          3、t=Template( 模板文件名 )?
          ?c=Context( 模板變量內容 )
          ?t.render(c)# 可以輸出模板內容
          4、?? 下面這段不理解什么意思
          ?To prevent this, set a function attribute alters_data on the method. The template system won ’ t execute a method if the method has alters_data=True set. For example:
          ?def delete(self):
          ??# Delete the account
          ?delete.alters_data = True
          5、 Context 對象支持 push()/pop() 方法
          6、 模板文件中的標簽:
          ?沒有 elseif;
          ?For 循環中沒有 break 和 continue
          ?For 循環中的幾個屬性:
          ??? forloop.counter???? # 當前循環的次數,從 1 開始
          ??? forloop.counter0??? # 當前循環的次數,從 0 開始
          ??? forloop.revcounter??????? # 當前循環剩余次數,從總循環次數遞減
          ??? forloop.revcounter0?????? # 當前循環剩余次數,從總循環次數 -1 遞減
          ??? forloop.first???????????? #boolean 值,如果為第一次循環,值為真
          ??? forloop.last????????????? # 同上
          ??? forloop.parentloop??????? # 引用父循環的 forloop 對象
          ?ifequal A B? # AB 只能是模板變量、字符串、數字
          ??? pass #如果 A B 相等則執行
          ?else
          ??? pass #否則執行
          ?endifequal
          ?{#?? #}????? #注釋
          ?{{A|B:”s”}}???????? # 對 A 執行 B 過濾, B 過濾可以有參數
          ?? 幾個過濾器:
          ?? addslashes??? 加反斜杠
          ?? Date????????? 格式化日期為字符串
          ?? escape??????? 轉換為網頁編碼
          ?? length??????? 長度
          7、 模板不能建立一個變量或者改變一個變量的值;不能調用原生的 python 代碼
          8、 在 setting.py 中制定模板文件存放的目錄( EMPLATE_DIRS ),例:
          ?TEMPLATE_DIRS = (
          ??'/home/django/mysite/templates',
          ?)
          ?不要忘了最后的逗號,除非你將序列()換成列表 [] ,但效率會降低;目錄用 / 間隔
          9、 使用模板:
          ?from django.shortcuts import render_to_response
          ?import datetime

          ?def current_datetime(request):
          ??now = datetime.datetime.now()
          ?return render_to_response('current_datetime.html', {'current_date': now})
          ?可以將填充到模板的變量換為locals(),但性能會有所下降,如
          ?def current_datetime(request):
          ??current_date = datetime.datetime.now()
          ????return render_to_response('current_datetime.html', locals())
          10、如果要引用設定的模板目錄中子目錄的模板文件 ;
          ?t = get_template('dateapp/current_datetime.html')
          11、模板可嵌套,模板文件名可用變量
          ?{% include 'includes/nav.html' %}
          ?{% include template_name %}
          12、模板繼承,使用 extends 和一個特殊的標簽 block ,例:
          ?#base.html
          ?<head>
          ?<title>
          ??{% block title %}標題{% endblock %}
          ?</title>
          ?</head>
          ?<body>
          ?{% block content %}內容{% endblock %}
          ?{% block footer %} 頁尾{% endblock %}
          ?</body>
          ?</html>
          ? 下面的模板繼承自 base.html
          ?{% extends "base.html" %}???? #這一行必須是第一個模板標簽行
          ?{% block title %} 我的標題 {% endblock %}
          ?{% block content %}
          ??<p> 我的內容 </p>
          ?{% endblock %}?? #不一定要重新定義父模板中的每個模板塊
          ?通過 block.super 引用父模板塊內容

          主站蜘蛛池模板: 顺昌县| 德阳市| 格尔木市| 霍邱县| 武邑县| 永新县| 运城市| 德兴市| 揭阳市| 天全县| 大厂| 香格里拉县| 汉寿县| 会理县| 三亚市| 滨州市| 济宁市| 珲春市| 石楼县| 平遥县| 陕西省| 延安市| 施秉县| 竹山县| 南通市| 石阡县| 盘锦市| 绥江县| 休宁县| 巫山县| 大石桥市| 昭平县| 高台县| 白山市| 姚安县| 山东| 应用必备| 荣成市| 绩溪县| 津市市| 洪湖市|