Django实战(二)之模板语言

摘要:
添加hello.html<模板中;{{你好}}<(1) 如果/else或如果/elseif/else标签{%ifcondition%}显示{%endif%}或{%If condition1%}显示1{%elifcondition2%}。。。display2{%selse%}。。。标签<的Display3{%endif%}(2);

该实战教程基于菜鸟教程,菜鸟教程可参考: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实战(二)之模板语言第1张

这里说一下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的区别外,那就只能说{和%区别。

 

免责声明:文章转载自《Django实战(二)之模板语言》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇Redis服务设计(处理流程、事件模型、多路复用)运维笔试题-简答题下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

字典、列表之间相互嵌套

列表中嵌套字典 employee_1 = {'name': 'david', 'dept': 'ops', 'post': 'NOC', 'salary': 12000, 'id': 113} employee_2 = {'name': 'brain', 'dept': 'auto', 'post': 'DBA', 'salary': 13000, 'id...

postcss-pxtorem插件相关配置

功能作用:将px像素自动转化为rem。 首先安装postcss-pxtorem。 1 npm install postcss-pxtorem -D 新建一个postcss.config.js文件配置。 1 module.exports = { 2 plugins: { 3 // 兼容浏览器,添加前缀 4 au...

pom文件

1.父子pom 如果在一个工程中分多个模块,那么会有父子pom。一般子pom中会有配置,指示其依赖的父pom: <parent>    <groupId>com.xxx</groupId>    <artifactId>xxx</artifactId>    <version>1.1....

将某个div内容保存成图片,使用html2canvas截图方法(高清图并解决图片跨域问题)

首先附上html2canvas的CDN地址:http://www.bootcdn.cn/html2canvas/ ; 此方法可截取整个div的内容,包括不可视区域,并且可以实现跨域图片截图。之前看了很多关于html2canvas插件图片跨域的解决办法,大部分的答复是在服务器端配置,之后将useCORS属性设置为true,但是如果图片是保存在别人家的服务器上...

SpringBoot application.yml logback.xml,多环境配置,支持 java -jar --spring.profiles.active

趁今天有时间整理了一下 启动命令为 //开发环境 java -jar app.jar --spring.profiles.active=dev--server.port=8060 //测试环境 java -jar app.jar --spring.profiles.active=qa --server.port=8060 //生产环境 java -jar...

微信小程序最新获取用户头像昵称的方法

官方提供的最新方法Open-data标签,使用这个标签可以不用用户授权直接获得头像和昵称 微信小程序获取用户信息的两种方法wx.getUserInfo&open-data https://blog.csdn.net/lucky_Zeng/article/details/80066479...