Django学习-5-模板渲染

Posted 陈乾

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Django学习-5-模板渲染相关的知识,希望对你有一定的参考价值。

1. {{ 变量名 }}
        
                def func(request):
                    return render(request, "index.html", {‘current_user‘: "alex"})
        
                    
                index.html
                
                <html>
                ..
                    <body>
                        <div>{{current_user}}</div>
                    </body>
                
                </html>
                
                ====> 最后生成的字符串
 
2.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>
                
  3. 索引
                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>
            
4.条件判断
            
                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>
                
                默认循环字典时,取的是key
                {%for i in dic%}
                    {{i}}            keys
                 {%endfor%}
 
                {%for i in dic.keys%}
                    {{i}}            keys            键
                 {%endfor%}
 
                  {%for i in dic.values%}
                    {{i}}            vaules          值
                 {%endfor%}
            
                 {%for i,j in dic.items%}    键值对
                    {{i}},{{j}}            (key,vaules)    
                 {%endfor%}

以上是关于Django学习-5-模板渲染的主要内容,如果未能解决你的问题,请参考以下文章

django学习第三天

Django学习系列(三.模板template)

Django 学习第四天——Django 模板标签

django学习第81天Django模板层2

06-Django-基础篇-模板

如何解析和渲染 json 以在 Django 模板中使用