Django 1.6.5模板搭建

django-admin.py startproject HelloWorld

按照下图创建响应文件和目录

hello.html文件

<h1>{{hello}}</h1>

在HelloWorl/setting.py添加TEMPLATES中的内容

TEMPLATE_PATH="F:...../templates/"

TEMPLATE_DIRS = (
    # Put strings here, like "/home/html/django_templates".
    # Always use forward slashes, even on Windows.
    #"./templates",
    TEMPLATE_PATH,
    # TEMPLATE_PATH+"/common",
    # TEMPLATE_PATH+"/tts_base",
    # TEMPLATE_PATH+"/tts_check",
    # TEMPLATE_PATH+"/tts_model",
这些是对应工程中的配置路径,这个可以找到我们添加到目录的模板

view.py的内容修改

from django.shortcuts import render


# def hello(request):
#    return HttpResponse("Hello World")

def hello(request):
    context = {}
    context['hello'] = 'Hello World Using Template!'
    return render(request,'hello.html',context)
使用render代替HttpResponse,同事render使用一个字典context作为参数。context字典中元素的值“hello”对应模板中的变量"hello"

在urls.py添加

from HelloWorld.view import hello
('^hello/$',hello),
访问127.0.0.1/hello/


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值