模板

前面的例子中,我们是直接将HTML写在了Python代码中,这种写法并不可取。我们需要使用模板技术将页面设计和Python代码分离。

模板通常用于产生HTML,但是Django的模板也能产生任何基于文本格式的文档。

参考http://djangobook.py3k.cn/2.0/chapter04/,对以下例子模板:

<html>
<head><title>Ordering notice</title></head> <body> <h1>Ordering notice</h1> <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> <p>Here are the items you've ordered:</p> <ul>
{% for item in item_list %}
<li>{{ item }}</li>
{% endfor %}
</ul> {% 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> </body>
</html>
  • 用两个大括号括起来的文字(例如 {{ person_name }} )被成为变量。意味着在此处插入指定变量的值。
  • 被大括号和百分号包围的文本(例如 {% if ordered_warranty %} )是模板标签,通知模板系统完成耨写工作的标签。这个例子中有包含if和for两种标签。
  • if标签就是类似Python中的if语句;而for标签就是类似Python中的for语句。
  • 这个例子中还使用了filter过滤器((例如 {{ ship_date|date:"F j, Y" }}),它是一种最便捷的转换变量输出格式的方式。

使用模板系统

在Python代码中使用Django模板的最基本方式如下:

  • 可以用原始的模板代码字符串创建一个 Template 对象;
  • 调用模板对象的render方法,并且传入一套变量context。模板中的变量和标签会被context值替换。

例子如下:

>>> from django import template
>>> t = template.Template('My name is {{ name }}.')
>>> c = template.Context({'name': 'Adrian'})
>>> print t.render(c)
My name is Adrian.
>>> c = template.Context({'name': 'Fred'})
>>> print t.render(c)
My name is Fred.

创建模板文件:

我们先给我们的testapp添加一个add视图,即在views.py中添加:

def add(request):
return render(request, 'add.html')

在testapp目录下新件一个templates文件夹,里面新建一个add.html。默认情况下,Django会默认找到app目录下的templates文件夹中的模板文件。

然后在add.html中写一些内容:

<!DOCTYPE html>
<html>
<head>
<title>欢迎光临</title>
</head>
<body>
Just Test
</body>
</html>

更新url配置,即在urls.py中添加:

from testapp.views import add

urlpatterns = [
......
url(r'^add/$', add),
]

重启服务后便可以访问http://127.0.0.1:8000/add/

接下来尝试在html文件中放入动态内容。

显示一个基本的字符串在网页上。

views.py:

def add(request):
string = "这是传递的字符串"
return render(request, 'add.html', {'string': string})

即向add.html传递一个字符串名称为string。在字符串中这样使用:

add.html

{{ string }}

使用for循环显示list

修改views.py:

def add(request):
testList = ["Python", "C++", "JAVA", "Shell"]
return render(request, 'add.html', {'testList': testList})

修改add.html:

    {% for i in testList %}
{{i}}
{% endfor %}

显示字典中内容

修改views.py:

def add(request):
testDict = {"1":"Python", "2":"C++", "3":"JAVA", "4":"Shell"}
return render(request, 'add.html', {'testDict': testDict})

修改add.html:

    {% for key,value in testDict.items %}
{{key}} : {{value}}
{% endfor %}

更多有用的操作可以参考https://code.ziqiangxuetang.com/django/django-template2.html

django+sqlite进行web开发(二)的更多相关文章

  1. django+sqlite3进行web开发(一)

    服务器配置 安装django sudo apt-get install python-django -y 安装mysql(可选) 也可以直接使用sqlite sudo apt-get install ...

  2. 跟阿根一起学Java Web开发二:使用Ajax技术及XML与JSON实现输出

    如今B/S结构的系统使用Ajax技术是再平常只是的了.今天我们就来探讨下在JSPGenSDF第四版中:怎样使用Ajax技术.怎样输出XML文件及JSON格式数据输出. 怎样搭建一个最基础的JSPGen ...

  3. 【Django】Python web开发:几个模板系统的性能对比(转)

    http://blog.chedushi.com/archives/910 结论: 点评一下吧.django就是个渣,不多废话了.webpy的代码很简洁,可惜速度太慢了.bottle看起来快一点,不过 ...

  4. web开发(二) Servlet中response、request乱码问题解决

    在网上看见一篇不错的文章,写的详细. 以下内容引用那篇博文.转载于<http://www.cnblogs.com/whgk/p/6412475.html>,在此仅供学习参考之用. 一.re ...

  5. Django个人博客开发 | 前言

    本渣渣不专注技术,只专注使用技术,不是一个资深的coder,是一个不折不扣的copier 1.前言 自学 Python,始于 Django 框架,Scrapy 框架,elasticsearch搜索引擎 ...

  6. [Python] 利用Django进行Web开发系列(二)

    1 编写第一个静态页面——Hello world页面 在上一篇博客<[Python] 利用Django进行Web开发系列(一)>中,我们创建了自己的目录mysite. Step1:创建视图 ...

  7. 使用eclipse搭建第一个python+Django的web开发实例

    python+Django的web开发实例   一.创建一个项目如果这是你第一次使用Django,那么你必须进行一些初始设置.也就是通过自动生成代码来建立一个Django项目--一个Django项目的 ...

  8. Django Web开发【4】 用户注册与管理

    几乎所有的网站都提供了用户注册与管理功能,这一节,我们将讲解如何利用Django自身提供的用户认证系统实现用户注册与管理功能. 会话认证 在上一节中,我们学习了User数据模型,并用它来保存用户信息, ...

  9. [python] python django web 开发 —— 15分钟送到会用(只能送你到这了)

    1.安装python环境 1.1 安装python包管理器: wget https://bootstrap.pypa.io/get-pip.py sudo python get-pip.py   1. ...

随机推荐

  1. 【领会要领】web前端-轻量级框架应用(jQuery基础)

    作者 | Jeskson 来源 | 达达前端小酒馆 jquery的安装和语法,jquery的多种选择器,dom操作和jquery事件. jQuery框架,简介,优势,安装,语法,jQuery选择器,i ...

  2. [LeetCode] 378. Kth Smallest Element in a Sorted Matrix 有序矩阵中第K小的元素

    Given a n x n matrix where each of the rows and columns are sorted in ascending order, find the kth ...

  3. [LeetCode] 374. Guess Number Higher or Lower 猜数字大小

    We are playing the Guess Game. The game is as follows: I pick a number from 1 to n. You have to gues ...

  4. 第20课 unique_ptr独占型智能指针

    一. unique_ptr的基本用法 (一)初始化方式 1. 直接初始化:unique<T> myPtr(new T);  //ok.但不能通过隐式转换来构造,如unique<T&g ...

  5. 3 datax mysql和hive之间相互导入

                                                mysql-->hive     0 参考文档: https://github.com/alibaba/D ...

  6. Python3.7 exe编译工具对比zz

    For years, NVDA has used Py2exe to package Python code into something that is executable on a system ...

  7. visual studio远程调试 remote debugger

    下载远程debug工具: https://docs.microsoft.com/zh-cn/visualstudio/debugger/remote-debugging?view=vs-2015 或者 ...

  8. 实战django(二)--登录实现记住我

    上节初步实现了登录和注册模块,这节我们进一步实现“记住我”功能. 大体功能分为以下模块: 1.在登录时如果勾选记住我,那么就将用户username存进cookie中,跳转到index页面: 2.此时, ...

  9. 出师表(ENGLISH) 强烈打call啊~王洛勇是什么神仙英语

    臣亮言:先帝创业未半而中道崩殂, Permit me to observe: the late emperor was taken from us before he could finish his ...

  10. nginx反向代理配置去除前缀

    (转载)原文链接:https://blog.csdn.net/gongchenyu/article/details/85960027 使用nginx做反向代理的时候,可以简单的直接把请求原封不动的转发 ...