Django 1.6.5模板搭建

来源:互联网 发布:封面ps软件 编辑:程序博客网 时间:2024/06/06 02:17

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