99热99这里只有精品6国产,亚洲中文字幕在线天天更新,在线观看亚洲精品国产福利片 ,久久久久综合网

歡迎加入QQ討論群258996829
麥子學(xué)院 頭像
蘋果6袋
6
麥子學(xué)院

Django學(xué)習(xí)之模板標(biāo)簽詳解

發(fā)布時(shí)間:2017-09-11 12:56  回復(fù):0  查看:2274   最后回復(fù):2017-09-11 12:56  
本文和大家分享的主要是django 中模板標(biāo)簽相關(guān)內(nèi)容,一起來看看吧,希望對(duì)大家 學(xué)習(xí)django有所幫助。
   1.IF標(biāo)簽
  Hello World/views.py
  from django.shortcuts  import render
  2
  class  Person(object):
  4      def  __init__(self,name,age,sex):
  5         self.name=name
  6         self.age=age
  7         self.sex=sex
  8
9      def  say(self):
10          return "I'm %s." %self.name
11         
12  def  index(request):
13     # 傳入普通變量
14     # 傳入數(shù)據(jù)為  html  中的變量: views 中的變量
15     #return render(request,'index.html',{'title':'Welcome','name':'KeinLee'})
16
17     # 傳入字典變量
18     person = {'name':'Lee','age':20,'sex':'male'}
19     # 傳入列表變量
20     book_list =['Python','Java','C']
21     #book_list =[]
22     # 傳入對(duì)象變量
23     #person=Person('Lucky',18,'female')
24      return render(request,'index.html',{'title':'Welcome','person':person,'book_list':book_list})
  views.py
  (1 if ...elif...else...endif
  Hello World/temlplates/index.html
  {%  if book_list.0 == 'Java' %}
  第一本書是Java{%  elif book_list.0 == 'Python' %}
  第一本書是Python{%  else %}
  第一本書是C{%  endif %}
  View Code
  結(jié)果為:第一本書是Python
 ?。?/span>2 if...and...or...not...endif
  注意:and or 可以同用,但是 and 的優(yōu)先級(jí)比 or
  Hello World/temlplates/index.html
  {%  if book_list or person %}
  存在book_list  或者  person{%  endif %}
  {%  if book_list and person %}
  book_list  和  person 都存在 {%  endif %}
  {%  if book_list and not person %}
  存在book_list  不存在 person{%  endif %}
  View Code
  結(jié)果為:存在book_list  或者  person book_list  和  person 都存在、(空)
  (3 if 符號(hào)運(yùn)算( == 、 != 、 > 、 >= <= 、 < 、 in 、 not in 、 is 、 is not
  if is XX True 這個(gè)是當(dāng)且僅當(dāng) XX 為真,這個(gè)暫時(shí)理解不了
  {%  if book_list.0 == 'Python' %}
  1. 第一本書是 Python{%  endif %}
  {%  if book_list.0 != 'Python' %}
  2. 第一本書不是 Python{%  endif %}
  {%  if person.age <= 20  %}
  3. 這個(gè)人的年齡沒超過 20{%  else %}
  4. 這個(gè)人的年齡超過 20{%  endif %}
  {%  if  'Python'  in book_list %}
  5.Python book_list 列表里 {%  endif %}
  {%  if  'Py' not  in book_list %}
  6.Py book_list 列表里 {%  endif %}
  {%  if  book_list.4 is not True %}
  7.book_list.4 不存在 {%  endif %}
  {%  if  book_list is not None%}
  8.book_list 列表存在 {%  endif %}
  View Code
  結(jié)果:1 、  3 、  5 、  6 、 7 、 8 能夠顯示
   2.For標(biāo)簽
 ?。?/span>1 )列表 for 循環(huán)
  {%  for book  in book_list %}{{book}}{%  endfor %}
  結(jié)果:Python Java C
  (2 )字典 for 循環(huán)
  {%  for k,v  in person.items %}{{k}}:{{v}}{%  endfor %}
  結(jié)果:sex:male name Django學(xué)習(xí)之模板標(biāo)簽詳解ee age:20
 ?。?/span>3 for...empty   (在 views.py 中沒有定義 book_list2
  {%  for book  in book_list2 %}{{book}}{%  empty %}
  沒有這個(gè)列表或者該列表為空{%  endfor %}
  結(jié)果:沒有這個(gè)列表或者該列表為空
 ?。?/span>4 forloop
  forloop.counter  循環(huán)記數(shù),默認(rèn) 1 開始
  forloop.counter0  循環(huán)記數(shù),從 0 開始
  forloop.revcounter  循環(huán)到記數(shù),默認(rèn) 1 結(jié)束
  forloop.revcounter0  循環(huán)記數(shù),到 0 結(jié)束
  forloop.first  第一次循環(huán) bool 值為 True ,一般與 if 連用
  forloop.last  最后一次循環(huán) bool 值為 True ,一般與 if 連用
  forloop.parentloop  循環(huán)嵌套中對(duì)上一層循環(huán)的操作
  {%  for k  in person %}{% if forloop.first %}
  這是第一次循環(huán){%  elif forloop.last%}
  這是最后一次循環(huán){%  else %}{{k}}{%  endif %}{%  endfor %}
  結(jié)果:這是第一次循環(huán) name  這是最后一次循環(huán)
來源: 博客園
您還未登錄,請(qǐng)先登錄

熱門帖子

最新帖子

?