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/


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章