第一步 创建项目文件:

    django-admin.py startproject ***

第二步 进入该文件下创建文件夹templates,在该文件夹下创建thanks.html

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Odering Notice</title>
</head>
<body> <h1>Ordering Notice</h1> <p>Dear {{person_name}},</p> <P>Thanks for placing an oder form {{company}}.It's scheduled
ship on {{ship_date|date:"F j,Y"}}</P> <P>Here are the items you've order:</P> <U>
{% for item in item_list %}
<li>{{item}}</li>
{% endfor %}
</U> {% if ordered_warranty %}
<p>Your warranty information will be included in tne packaging. </p>
{% else %}
<p>You didn't order a warranty, so you're on your own when
the products inevitably stop working.</p>
{% endif %} <p>Sincerely,<br/>{{company}}</p> </body>
</html>

第三步 修改settings.py

第四步 新建views.py(与settings.py处于同一文件夹下)

from django.http import HttpResponse,Http404
import datetime
from django.shortcuts import render_to_response def thanks(request):
return render_to_response("1.html",{'person_name':'ABC','company':'ABC','ship_date':datetime.date(2017,12,24),'ordered_warranty':True})

第五步 修改urls.py(这里我修改了代码,红色部分)

from django.conf.urls import url
from myThirdproject.views import current_datetime,hours_ahead,hello,thanks,current_datetime1,hours_ahead1 urlpatterns = [
url(r'^thanks/$', thanks),
]

第六步 启动server

  python manage.py runserver

OK 搞定!!!!

以上介绍了django的简单数据绑定,详细内容大家可以百度文档,下面来介绍一下django是如何实现以上功能的,首先,windows键+R进入DOS,输入:python manage.py shell,进入python交互界面(注意如果要在pycharm里面运行还要配置环境变量比较麻烦),输入以下:

>>> from django.template import Template, Context
>>> raw_template = """<p>Dear {{ person_name }},</p>
...
... <p>Thanks for placing an order from {{ company }}. It's scheduled to
... ship on {{ ship_date|date:"F j, Y" }}.</p>
...
... {% if ordered_warranty %}
... <p>Your warranty information will be included in the packaging.</p>
... {% else %}
... <p>You didn't order a warranty, so you're on your own when
... the products inevitably stop working.</p>
... {% endif %}
...
... <p>Sincerely,<br />{{ company }}</p>"""
>>> t = Template(raw_template)
>>> import datetime
>>> c = Context({'person_name': 'John Smith',
... 'company': 'Outdoor Equipment',
... 'ship_date': datetime.date(2009, 4, 2),
... 'ordered_warranty': False})
>>> t.render(c)
u"<p>Dear John Smith,</p>\n\n<p>Thanks for placing an order from Outdoor
Equipment. It's scheduled to\nship on April 2, 2009.</p>\n\n\n<p>You
didn't order a warranty, so you're on your own when\nthe products
inevitably stop working.</p>\n\n\n<p>Sincerely,<br />Outdoor Equipment
</p>"

下面来解释一下结果为什么是这样:

  首先我们导入 (import)类 Template 和 Context ,它们都在模块 django.template 里。
  我们把模板原始文本保存到变量 raw_template 。注意到我们使用了三个引号来 标识这些文本,因为这样可以包含多行。
  接下来,我们创建了一个模板对象 t ,把 raw_template 作为 Template 类构造函数的参数。我们从Python的标准库导入 datetime 模块,以后我们将会使用它。
  然后,我们创建一个 Context 对象, c 。 Context 构造的参数是Python 字典数据类型。 在这里,我们指定参数 person_name 的值是 'John Smith', 参数company 的值为‘Outdoor Equipment’ ,等等。
  最后,我们在模板对象上调用 render() 方法,传递context参数给它。 这是返回渲染后的模板的方法,它会替换模板变量为真实的值和执行块标签。
  注意,warranty paragraph显示是因为 ordered_warranty 的值为 True. 注意时间的显示, April 2, 2009, 它是按 'F j, Y' 格式显示的。

一旦有了 模板对象,你就可以通过它渲染多个context, 例如:
>>> from django.template import Template, Context
>>> t = Template('Hello, {{ name }}')
>>> print t.render(Context({'name': 'John'}))
Hello, John
>>> print t.render(Context({'name': 'Julie'}))
Hello, Julie
>>> print t.render(Context({'name': 'Pat'}))
Hello, Pat

django第三课 模版的更多相关文章

  1. 【Web探索之旅】第二部分第三课:框架和内容管理系统

    内容简介 1.第二部分第三课:框架和内容管理系统 2.第二部分第四课预告:数据库   第二部分第三课:框架和内容管理系统 上一课我们介绍了服务器端的编程语言,有PHP,Java,Python,Ruby ...

  2. CodeIgniter框架入门教程——第三课 URL及ajax

    本文转载自:http://www.softeng.cn/?p=74 这节课讲一下CI框架的路由规则,以及如何在CI框架下实现ajax功能. 首先,先介绍CI框架的路由规则,因为CI框架是在PHP的基础 ...

  3. SQL初级第三课(下)

    我们续用第三课(上)的表 辅助表 Student                   Course               Score                    Teacher Sno ...

  4. shellKali Linux Web 渗透测试— 初级教程(第三课)

    shellKali Linux Web 渗透测试— 初级教程(第三课) 文/玄魂 目录 shellKali Linux Web 渗透测试—初级教程(第三课) 课程目录 通过google hack寻找测 ...

  5. 【第三课】ANR和OOM——贪快和贪多的后果(下)

    Out of Mana,法力耗尽. 内存就像法力,耗尽了就什么都不能做了.有时候一个应用程序占用了太大的内存,超过了Android系统为你规定的限制,那么系统就会干掉你,以保证其他app有足够的内存. ...

  6. 【第三课】ANR和OOM——贪快和贪多的后果(上)

    恼人的ANR 早先年用Android的时候,就连很多知名的app也总是莫名其妙崩溃,好像手机快的时候会崩溃,手机卡的时候app会卡死.卡死的时候会弹出来一个框,询问是要结束app还是继续等待.这就是A ...

  7. NeHe OpenGL教程 第三课:颜色渲染

    转自[翻译]NeHe OpenGL 教程 前言 声明,此 NeHe OpenGL教程系列文章由51博客yarin翻译(2010-08-19),本博客为转载并稍加整理与修改.对NeHe的OpenGL管线 ...

  8. BeagleBone Black 板第三课:Debian7.5系统安装和远程控制BBB板

    BBB板第三课:Debian7.5系统安装和远程控制BBB板 由于BBB板系统是Debian 7.4.据说使用Debian系统能够实现非常多BBB板的无缝连接.能够更好的学习和控制BBB板,所以就决定 ...

  9. 【Linux探索之旅】第二部分第三课:文件和目录,组织不会亏待你

    内容简介 1.第二部分第三课:文件和目录,组织不会亏待你 2.第二部分第四课预告:文件操纵,鼓掌之中 文件和目录,组织不会亏待你 上一次课我们讲了命令行,这将成为伴随我们接下来整个Linux课程的一个 ...

随机推荐

  1. 解决Docker时区与主机时区不一致的问题

    在Dockerfile里面增加以下红色的部分 FROM hub.chinacloud.com/common/jdk:8MAINTAINER xxx@chinacloud.com.cn RUN mkdi ...

  2. java反编译工具jad使用

    参考:http://blog.csdn.net/u014472711/article/details/53713269 http://lijingshou.iteye.com/blog/2005717 ...

  3. Initialization of bean failed; nested exception is org.springframework.beans.InvalidPropertyException: Invalid property 'dataSource' of bean class [com.liuyang.jdbc.PersonDao]: No property 'dataSource

    这个错误是说我的启动失败了.这类问题要从配置文件中开始找原因,我用的是spring框架,所以我从application.中找的原因 然后对比路径,对比文件的命名和id,都没有问题,为什么会在启动的时候 ...

  4. 基于图像切换器(imageSwitcher)的支持动画的图片浏览器

    利用GridView和ImageSwitcher的基本用法 public class MainActivity extends Activity { int[] imageIds = new int[ ...

  5. spring @Transactional 声明式事务

    项目地址:git@github.com:witaste/transaction-annotation.git 情景一: A external method calls a method of the ...

  6. SED 学习笔记

    1. Sed简介   sed是一种在线编辑器,它一次处理一行内容.处理时,把当前处理的行存储在临时缓冲区中,称为“模式空间”(pattern space),接着用sed命令处理缓冲区中的内容,处理完成 ...

  7. Eclipse ADT 与VS 常用的快捷键 对比学习

    注:以下说的类型于VS,是指:VS+Resharper的快捷键,我是采用了Resharper作为VS的快捷键. 导航 Ctrl+1 快速修复 (类似于VS的alt+enter) Ctrl+D: 删除当 ...

  8. [工具]JSON校验、转换在线工具

    1. 在线JSON代码检验.检验.美化.格式化工具[简单易用的格式化工具]: http://tools.jb51.net/code/json 2. JSON在线格式化工具[代码高亮及可控缩进大小的格式 ...

  9. 巧用XML配置校验导入Excel的列数据格式

    <?xml version="1.0"?> <ColumnsSeting xmlns:xsd="http://www.w3.org/2001/XMLSc ...

  10. C#MVC和cropper.js实现剪裁图片ajax上传的弹出层

     首先使用cropper.js插件,能够将剪裁后的图片返回为base64编码,后台根据base64编码解析保存图片. jQuery.cropper: 是一款使用简单且功能强大的图片剪裁jquery插件 ...