• Djago模板拾起


    在view中使用template:
    首先在settings.py中配置模板文件的路径。
    TEMPLATE_DIRS = (
    '/home/django/mysite/templates',
    )
    
    
    
    1.变量的使用
      {{ username }}
    
    2.条件语句使用
      if 可以使用and, or, not来组织你的逻辑。但不允许and和or同时出现的条件语句中。
      没有{% elif %}这样的用法,只能用嵌套来实现多重if语句。
      {% if is_logins %}
        {{ is_login }}
      {% else  %}
        <p> 123 </p>
      {% endif %}
    
    3.循环语句  使用empty关键字来进行为空时候的跳转。
      {% for item in date_all %}
        <li>{{ item.name }}</li>
      {% empty %}
        <li>你输入的为空奥</li>
      {% endfor %}
    
    
    
    4. ifequal和ifnotequal,一看就是直接比较值的tag,需要两个参数,用法比较有限,
        只限于字符串,整数,小数的比较,什么列表,字典,元组不支持。
    
          {% ifequal post.title post.body %}
                      <h1>Welcome!</h1>
                      {% endifequal %}
    
    5.{#  #},模板注释的用法,只能用在一行,如果要使用多行注释,要使用{% comment %}
          {% comment %}
            hello !word!!这里是注释内容
    
          {% endcomment %}
    
    
    6. 后端模板使用
    
      1.render_to_response
        大多数情况下,你会使用一种shortcut方法,render_to_response()去完成以上的工作。
        from django.shortcuts import render_to_response
      2.locals() 的使用
        如果你有很多变量传给render,一个一个构造DICT元素很麻烦,直接把变量名改成模板中所需的变量在使用locasl()函数,轻松搞定。
    
      3.{% include %}的使用
        {% include 'index.html' %},用来引入其它模板的内容,减少重复的模板代码
        return render_to_response('index.html',{'blog_list':'text.html'})
        {% include blog_list %} 可以传来变量名
        更好用的方法下
      4.{% block content %}   ##母板内容,
          {% endblock %}
          {% extends 'base.html' %}子版调用得导入母板,模板继承
              {% block content %}
                    子版内容
                {% endblock %}
    

      

  • 相关阅读:
    一句话解释数字签名。一句话解释数字证书
    KVC、KVO实现过程
    SSH基本概念和用途
    UICollectionView左对齐流水布局、右对齐流水布局
    Git命令学习笔记
    Xcode8插件安装
    Xcode日常使用
    dispatch_group_t 日常使用注意事项
    二分查找变种
    maven插件调试方法
  • 原文地址:https://www.cnblogs.com/pythonxiaohu/p/5842097.html
Copyright © 2020-2023  润新知