第三章、模板

1、模板的基本使用:

  

2、模板变量进行变量的查找:模板系统能够非常简洁地处理更加复杂的数据结构,例如list、 dictionary和自定义的对象。

  

 

 

 3、模板标签和过滤器:

  

    除以上几点以外的所有东西都视为 True。{% if %} 标签接受 and , or 或者 not 关键字来对多个变量做判断 ,或者对变量 取反( not )

    

 

 

     

 

 

       

 

 

 4、使用模板来来代替html的标签

 

 

 5、配置模板的路径和使用

  1)配置模板路径:打开setting.py,找到TEMPLATE_DIRS参数,修改为:

    TEMPLATE_DIRS = (

      os.path.join(os.path.dirname(__file__), 'templates').replace('\\','/'),

    )

  2)在mysite文件夹下新建模板文件夹:,再建一个time.html文件,写入代码:

<html><body>It is now {{ current_date }}.</body></html>

  3)修改new_time试图为:

from django.template.loader import get_template
def new_now_time(request):
    now = datetime.datetime.now()
    t = get_template('time.html')
    html = t.render({'current_date': now})
    return HttpResponse(html)

还可以使用另一种方式:

def new_now_time(request):
    now = datetime.datetime.now()
    return render_to_response('time.html', {'current_date': now})

还可以使用locals

def current_datetime(request):
    current_date = datetime.datetime.now()
    return render_to_response('time.html', locals())

 

 

 

   

 

posted @ 2022-02-04 19:05  燕岚风  阅读(26)  评论(0编辑  收藏  举报