zoukankan      html  css  js  c++  java
  • Django实战(二)之模板语言

    该实战教程基于菜鸟教程,菜鸟教程可参考:http://www.runoob.com/django/django-template.html

    模板语法,每个框架都有其支持的模板语法,Django的模板语法在我看来与vue.js倒有一些相似处

    ,比如它们的模板语法中参数为{{parm}}。

    本篇所用到的例子,仍然基于实战(一)

    在HelloWorld(该文件夹有urls.py)目录下新增templates文件夹,在templates新增hello.html

    <html>
    <head>
    </head>
    <body>
    <h1>{{ hello }}</h1>
    </body>
    </html>

    修改settings.py文件夹

    TEMPLATES = [
    {
    'BACKEND': 'django.template.backends.django.DjangoTemplates',
    'DIRS': [BASE_DIR+"/templates",], # 修改位置
    'APP_DIRS': True,
    'OPTIONS': {
    'context_processors': [
    'django.template.context_processors.debug',
    'django.template.context_processors.request',
    'django.contrib.auth.context_processors.auth',
    'django.contrib.messages.context_processors.messages',
    ],
    },
    },
    ]

    这个修改位置就好比Java的SSM开发中的视图解析器(可以是freemarker,可以是jsp,也可以是volocity等)。

    修改view.py

    # -*- coding: utf-8 -*-
    #from django.http import HttpResponse
    from django.shortcuts import render
    def hello(request):
    context = {}
    context['hello'] = 'Hello World!'
    return render(request, 'hello.html', context)

    这里的context与Java中的Map或者是Model倒也几分相似。

    简单的说,都是来返回给前端数据渲染用的。

    最后显示为:

    这里说一下Django的模板语言

    编程语言什么if-else ,if-else if -else,for 等等是十分比较常用的。

    而Django作为Python的web框架同样如此。

       (1)if/else或if/elseif/else标签

    {% if condition %}
         ... display
    {% endif %}

    {% if condition1 %}
       ... display 1
    {% elif condition2 %}
       ... display 2
    {% else %}
       ... display 3
    {% endif %}



    (2)for标签
    <ul>
    {% for athlete in athlete_list %}
        <li>{{ athlete.name }}</li>
    {% endfor %}
    </ul>

    (3)ifequal/ifnotequal 标签

    {% ifequal %} 标签比较两个值,当他们相等时,显示在 {% ifequal %} 和 {% endifequal %} 之中所有的值。

    下面的例子比较两个模板变量 user 和 currentuser :

    {% ifequal user currentuser %}
        <h1>Welcome!</h1>
    {% endifequal %}


    和 {% if %} 类似, {% ifequal %} 支持可选的 {% else%} 标签:8

    {% ifequal section 'sitenews' %}
        <h1>Site News</h1>
    {% else %}
        <h1>No News Here</h1>
    {% endifequal %}



    (4)注释标签
    {# 这是一个注释 #}


    (5)过滤器
    模板过滤器可以在变量被显示前修改它,过滤器使用管道字符,如下所示:
    {{ name|lower }}

    {{ name }} 变量被过滤器 lower 处理后,文档大写转换文本为小写。

    过滤管道可以被* 套接* ,既是说,一个过滤器管道的输出又可以作为下一个管道的输入

      (6)include 标签

    {% include "nav.html" %}

    同jsp中的<%include file="nav.html"%>
    要说区别除了Python与Java的区别外,那就只能说{和%区别。

     
  • 相关阅读:
    LeetCode 623. Add One Row to Tree
    LeetCode 894. All Possible Full Binary Trees
    LeetCode 988. Smallest String Starting From Leaf
    LeetCode 979. Distribute Coins in Binary Tree
    LeetCode 814. Binary Tree Pruning
    LeetCode 951. Flip Equivalent Binary Trees
    LeetCode 426. Convert Binary Search Tree to Sorted Doubly Linked List
    LeetCode 889. Construct Binary Tree from Preorder and Postorder Traversal
    LeetCode 687. Longest Univalue Path
    LeetCode 428. Serialize and Deserialize N-ary Tree
  • 原文地址:https://www.cnblogs.com/youcong/p/9439423.html
Copyright © 2011-2022 走看看