六狼论坛

 找回密码
 立即注册

QQ登录

只需一步,快速开始

新浪微博账号登陆

只需一步,快速开始

搜索
查看: 37|回复: 0

Django笔记3

[复制链接]

升级  90%

51

主题

51

主题

51

主题

秀才

Rank: 2

积分
185
 楼主| 发表于 2013-1-15 02:55:51 | 显示全部楼层 |阅读模式
1.更改settings.py设置

import os.path

TEMPLATE_DIRS = (
    os.path.join(os.path.dirname(__file__), 'templates').replace('\','/'),
)

2.对current_datetime进行修改

from django.template.loader import get_template
from django.template import Context
from django.http import HttpResponse
import datetime

def current_datetime(request):
    now = datetime.datetime.now()
    t = get_template('current_datetime.html')
    html = t.render(Context({'current_date': now}))
    return HttpResponse(html)

3.在模板目录中创建包括以下模板代码 current_datetime.html 文件:
It is now {{ current_date }}.
以上3步完成模板分离

4.更方便的使用
from django.shortcuts import render_to_response
import datetime

def current_datetime(request):
    now = datetime.datetime.now()
    return render_to_response('current_datetime.html', {'current_date': now})
您需要登录后才可以回帖 登录 | 立即注册 新浪微博账号登陆

本版积分规则

快速回复 返回顶部 返回列表