jilingxf的个人博客分享 http://blog.sciencenet.cn/u/jilingxf

博文

django学习笔记(2)——使用模板

已有 1827 次阅读 2019-2-17 19:25 |个人分类:python|系统分类:科研笔记| django, templates, render

django使用DTL DTL可以参考https://docs.djangoproject.com/en/1.11/ref/templates/language/文档

This document explains the language syntax of the Django template system. If you’re looking for a more technical perspective on how it works and how to extend it, see The Django template language: for Python programmers.
本文解释Django模板系统的语言语法。如果您正在寻找关于它如何工作以及如何扩展它的更技术性的观点,请参阅Django模板语言:for Python程序员。
Django’s template language is designed to strike a balance between power and ease. It’s designed to feel comfortable to those used to working with HTML. If you have any exposure to other text-based template languages, such as Smarty or Jinja2, you should feel right at home with Django’s templates.
Django的模板语言旨在在强大和简单之间取得平衡。它的目的是让那些习惯使用HTML的人感到舒适。如果您接触过其他基于文本的模板语言,比如Smarty或Jinja2,那么您应该对Django的模板非常熟悉。
Philosophy

If you have a background in programming, or if you’re used to languages which mix programming code directly into HTML, you’ll want to bear in mind that the Django template system is not simply Python embedded into HTML. This is by design: the template system is meant to express presentation, not program logic.
:如果您有编程背景,或者习惯了将编程代码直接混合到HTML中的语言,那么您需要记住Django模板系统不仅仅是嵌入到HTML中的Python。这是设计上的:模板系统是用来表达表示,而不是程序逻辑。
templates
A template is simply a text file. It can generate any text-based format (HTML, XML, CSV, etc.).
一个template只是一个文本文件。它可以生成任何基于文本的格式(HTML、XML、CSV等)。
A template contains variables, which get replaced with values when the template is evaluated, and tags, which control the logic of the template.
模板包含变量(在计算模板时将被值替换)和标记(控制模板的逻辑)。

使用步骤 在APP根目录下建立一个templates目录

在该目录下创建一个html文件 index.html

<!DOCTYPE html>
<html>
  <head>   <meta charset="UTF-8">
       <title>Title</title> 
 </head> 
 <body>
      <h1>hello blog!</h1>
 </body>
</html>

在view.py中返回render()

from django.shortcuts import render
from django.http import HttpResponse

# Create your views here.

def index(request):
    #return HttpResponse('hello word!')
    return render(request,'index.html')

刷新浏览器

render()方法

render(request, template_name, context=None, content_type=None, status=None, using=None)[source]¶ 
参考 https://docs.djangoproject.com/en/dev/topics/http/shortcuts/#render 
Django提供了一个函数render封装了代码 方法render包含3个参数 
    第一个参数为request对象 必填 
    第二个参数为模板文件路径 必填 
    第三个参数为字典,表示向模板中传递的上下文数据

render的字典值

要添加到模板上下文的值的字典。默认情况下,这是一个空字典。
如果dictionary中的值是可调用的,视图将在呈现模板之前调用它。
 该字典是后台传递到模板的参数,键名为参数名设置方法 {'键名':值},注意中间是冒号。如果写成“,”报错 
         详见: http://blog.sciencenet.cn/home.php?mod=space&uid=853805&do=blog&id=1162658
 在模板中使用{{键名}}来直接使用

例: views.py

from django.shortcuts import render
from django.http import HttpResponse

# Create your views here. 

def index(request):
    #return HttpResponse('hello word!') 
    return render(request,'index.html',{'hello':'hello blog key!'})

index.html

<!DOCTYPE html>
<html> 
    <head>     <meta charset="UTF-8">     <title>Title</title> </head>
    <body>
        <h1>{{ hello }}</h1>
    </body>
</html>

结果

两个APP设置templates问题

如果有两个或以上APP都使用template,需要在templates下加上一个与APP同名的文件夹,html文件放在这个文件夹内不会造成程序混乱问题。 
如果没有这样操作django按照urls.py中的urlpatterns添加顺序查找templates。
 如果是后更改的,使用pycharm时要注意,先改的APP内的views.py return语句被pycharm改错了,要改回来




https://wap.sciencenet.cn/blog-853805-1162663.html

上一篇:django render 字典值设置 报TTypeError 错误
下一篇:利用R GENLIB 计算近交系数
收藏 IP: 42.180.51.*| 热度|

0

该博文允许注册用户评论 请点击登录 评论 (0 个评论)

数据加载中...

Archiver|手机版|科学网 ( 京ICP备07017567号-12 )

GMT+8, 2024-6-3 00:55

Powered by ScienceNet.cn

Copyright © 2007- 中国科学报社

返回顶部