为了使网站更干净简洁更容易维护,页面的设计和Python的代码必须分离开.我们可以使用Django的 模板系统 (Template System)来实现这种模式. 几个简单的模板标签(tag): for 标签 {% for item in item_list %} 用于构建简单的循环,允许你遍历循环中的每一项 if 标签 {% if ordered_warranty %} 是用来执行逻辑判断的.在这个例子中标签检测 ordered_warranty 变量值是否为 True .如…
前言:当我们想在页面上给客户端返回一个当前时间,一些初学者可能会很自然的想到用占位符,字符串拼接来达到我们想要的效果,但是这样做会有一个问题,HTML被直接硬编码在 Python代码之中. 1 2 3 4 def current_datetime(request): now = datetime.datetime.now() html = "<html><body>It is now %s.</body></html>"…
继续之前的views,你可 能已经注意到我们例子中视图中返回的的方式有点特别.也就是说.HTML被直接硬编码在Python代码之中 def current_datetime(request): now = datetime.datetime.now() html = "<html><body>it is now %s</body></html>" %now return HttpResponse(html) 尽管这种技术便于解释视图是如何…
你可能已经注意到我们在例子视图中返回文本的方式有点特别. 也就是说,HTML被直接硬编码在 Python代码之中. def current_datetime(request): now = datetime.datetime.now() html = "<html><body>It is now %s.</body></html>" % now return HttpResponse(html) 尽管这种技术便于解释视图是如何工作的,但直…
来看一段代码 def current_datetime(request): now = datetime.datetime.now() html = "<html><body>It is now %s.</body></html>" % now return HttpResponse(html) 直接把HTML页面嵌套在视图函数里返回给浏览器并不是一个好主意: 原因: 1.对页面设计进行的任何改变都必须对 Python 代码进行相应的修改…