zoukankan      html  css  js  c++  java
  • Django之模板渲染

    8、模板渲染
    特殊的模板语言

    -- {{ 变量名 }}

    def func(request):
    return render(request, "index.html", {'current_user': "alex"}) 向对应的html传参数


    index.html

    <html>
    ..
    <body>
    <div>{{current_user}}</div>
    </body>

    </html>

    ====> 最后生成的字符串

    <html>
    ..
    <body>
    <div>alex</div>
    </body>

    </html>
    -- For循环
    def func(request):
    return render(request, "index.html", {'current_user': "alex", 'user_list': ['alex','eric']})

    index.html

    <html>
    ..
    <body>
    <div>{{current_user}}</div>

    <ul>
    {% for row in user_list %}

    {% if row == "alex" %}
    <li>{{ row }}</li>
    {% endif %}

    {% endfor %}
    </ul>

    </body>

    </html>

    #####索引#################
    def func(request):
    return render(request, "index.html", {
    'current_user': "alex",
    'user_list': ['alex','eric'],
    'user_dict': {'k1': 'v1', 'k2': 'v2'}})

    index.html

    <html>
    ..
    <body>
    <div>{{current_user}}</div>

    <a> {{ user_list.1 }} </a>
    <a> {{ user_dict.k1 }} </a>
    <a> {{ user_dict.k2 }} </a>

    </body>

    </html>

    ###### 条件

    def func(request):
    return render(request, "index.html", {
    'current_user': "alex",
    "age": 18,
    'user_list': ['alex','eric'],
    'user_dict': {'k1': 'v1', 'k2': 'v2'}})

    index.html

    <html>
    ..
    <body>
    <div>{{current_user}}</div>

    <a> {{ user_list.1 }} </a>
    <a> {{ user_dict.k1 }} </a>
    <a> {{ user_dict.k2 }} </a>

    {% if age %}
    <a>有年龄</a>
    {% if age > 16 %}
    <a>老男人</a>
    {% else %}
    <a>小鲜肉</a>
    {% endif %}
    {% else %}
    <a>无年龄</a>
    {% endif %}
    </body>

    </html>
  • 相关阅读:
    上周热点回顾(3.13.7)
    博客园电子期刊2010年1月刊发布啦
    博客园电子期刊2010年2月刊发布啦
    上周热点回顾(2.222.28)
    Android 专题上线
    聊聊2010年春晚
    上周热点回顾(3.83.14)
    博客园上海俱乐部活动通知(20100320)
    上周热点回顾(2.12.7)
    博客园图灵杯第4届博问大赛(2010.2.27~2010.3.27)
  • 原文地址:https://www.cnblogs.com/KingOfCattle/p/12712548.html
Copyright © 2011-2022 走看看