Django Templates的设置

1,为什么要有模板

从一个简单的例子开始,我们要网页上显示当前时间,视图的中函数如下:

def current_datetime(request):
    now = datetime.datetime.now()
    html = "<html><body>It is now %s.</body></html>" % now
    return HttpResponse(html)

很显然这段代码看起来有点怪
使用模板大致有以下几个优点:

  • 将业务逻辑的Python代码和页面设计的HTML代码分离
  • 使代码更干净整洁更容易维护
  • 使Python程序员和HTML/CSS程序员分开协作,提高生产的效率
  • 将HTML代码分离出来,使其能够复用
2,Django中怎么使用模板

当我们新建完project,新建完app,设置完settings.py和urls.py后,我把HTML文件放在哪里地方,也就是templates目录应该放在哪个地方,通常有两种方式:

  • 在应用的目录下新建templates
  • 在工程的目录下新建templates
3,应用下新建templates

这是Django默认的方式,意思就是一个应用会自动到本应用目录下的templates目录搜索html文件,或者说html文件对本应用的视图函数是透明可见的,不需要再去在settings.py设置TEMPLATE_DIRS,Django 1.8以上的版本做了显示的说明

TEMPLATES = [
  {
    'BACKEND': 'django.template.backends.django.DjangoTemplates',
    'APP_DIRS': True,
  },
]

例如我的工程是website,应用是blog,目录结构如下:

fage:website$ tree
.
├── blog
│   ├── admin.py
│   ├── admin.pyc
│   ├── __init__.py
│   ├── __init__.pyc
│   ├── migrations
│   │   ├── __init__.py
│   │   └── __init__.pyc
│   ├── models.py
│   ├── models.pyc
│   ├── templates
│   │   └── index.html
│   ├── tests.py
│   ├── urls.py
│   ├── urls.pyc
│   ├── views.py
│   └── views.pyc
├── db.sqlite3
├── manage.py
└── website
    ├── __init__.py
    ├── __init__.pyc
    ├── settings.py
    ├── settings.pyc
    ├── urls.py
    ├── urls.pyc
    ├── wsgi.py
    └── wsgi.pyc

views.py视图函数如下:

from django.shortcuts import render
from django.http import HttpResponse
# Create your views here.
def blog_index(request):
    content = {
    'test' : 'just for test.',
    'welcome': 'hello word'
    }
    return render(request, 'index.html', content)

index.html文件如下:

<p> {{ test }}</p>
<p> {{ welcome }} </p>

运行web服务器,就能在网页上显示对应的内容
在上面的基础上,通常还在temlates目录下新建一个同名app(名字可以不一样)的目录,主要将基础模板文件base.html放在外层
目录结构如下:

fage:blog$ tree templates/
templates/
├── base.html
└── blog
    └── index.html
1 directory, 2 files

之前的目录结构是

fage:blog$ tree templates/
templates/
└── index.html
0 directories, 1 file
fage:blog$

这时就要将views.py文件稍作修改:

from django.shortcuts import render
from django.http import HttpResponse
# Create your views here.
def blog_index(request):
    content = {
    'test' : 'just for test.',
    'welcome': 'hello word'
    }
    return render(request, 'blog/index.html', content)

总之一句话,app的views.py能够自动识别当前所有目录下templates目录下的文件,不要出现app的templates目录下出现同名文件,否则,有的不能识别,但不能递归识别,需要加上子目录的名字。
templates设置的相关参考:
VASKS
知乎

4,工程目录下设置templates

这种方式也比较常见,但是需要在settings.py中指定目录,不要忘了后面的逗号

TEMPLATE_DIRS = (
    os.path.join(BASE_DIR,'templates'),
)

当然使用绝对路径也可以,只不过灵活性差一些,当别人使用你的工程时,需要做一些调整,因为目录结构不一样

TEMPLATE_DIRS = (
    '/home/django/website1/templates',
)

这样当我们新建多个app的时候,/home/django/website1/templates目录下的html文件对任何一个视图函数来说都是透明的,例如:

fage:website1$ tree
.
├── account
│   ├── admin.py
│   ├── __init__.py
│   ├── migrations
│   │   └── __init__.py
│   ├── models.py
│   ├── tests.py
│   └── views.py
├── blog
│   ├── admin.py
│   ├── admin.pyc
│   ├── __init__.py
│   ├── __init__.pyc
│   ├── migrations
│   │   ├── __init__.py
│   │   └── __init__.pyc
│   ├── models.py
│   ├── models.pyc
│   ├── tests.py
│   ├── urls.py
│   └── views.py
├── db.sqlite3
├── manage.py
├── templates
│   ├── article.html
│   ├── base.html
│   └── login.html
└── website1
    ├── __init__.py
    ├── __init__.pyc
    ├── settings.py
    ├── settings.pyc
    ├── urls.py
    ├── urls.pyc
    ├── wsgi.py
    └── wsgi.pyc

这是temlates目录下的三个html文件对account和blog应用来说都是可见的,从三个文件来看,我们就知道base.html文件是基础模板,是account和blog都要用的文件,而login.html文件是account应用对应视图要用到的文件,article.html是blog应用对应视图要用的文件,在上面的基础上,通常我还需要对templates目录的文件结构做一次调整,新建blog目录,新建account目录

fage:website1$ tree templates/
templates/
├── account
│   └── article.html
├── base.html
└── blog
    └── login.html
2 directories, 3 files


文/秦汉邮侠(简书作者)
原文链接:http://www.jianshu.com/p/1c7812751d9d
著作权归作者所有,转载请联系作者获得授权,并标注“简书作者”。
  • 3
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值