[python web]django1.6 使用模板

1,在项目目录下建立templates文件夹(与manage.py在同个目录下),放入模板文件如page.html:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN">
<html lang="en">
<head>
    <title>Hello</title>
</head>
<body>
    <h1>test django template</h1>
    <p>Test for {{ content }}.</p>

    <hr>
    <p>Thank you!</p>
</body>
</html>


2,在settings.py中加入:

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


3,views.py中相关代码:

def test(request):
    
    task = "test"
    return render_to_response("page.html", {'content':task})

4,urls.py中加入:

     url(r'^test/', test),       

5,启动manage.py,在浏览器输入http://127.0.0.1:8000/test/   ,可看到页面。

你可能感兴趣的:([python web]django1.6 使用模板)