2015/08/20

Django Templates

本篇以Django Views and URL confs作為範本繼續新增Templates功能

首先在Project內新增資料夾templates,新增一個index.html網頁


index.html:
<!DOCTYPE html>
<html>
<meta charset="utf8">
<head>
 <title>{{title}}</title>
</head>
<body>
 <h1>{{info}}</h1>
</body>
</html>


接著將上一篇的app的views.py修改為如下方
from django.http import HttpResponse
from django.shortcuts import render


# Create your views here.
def myapp_hellow(request):
 return render(request,
    'index.html',
    {'title' : 'Welcome to my app',
    'info' : 'Hi, welcome to my app'})


接著到Project/settings.py找到TEMPLATES將DIRS修改為
'DIRS': [os.path.join(BASE_DIR, 'templates').replace('\\', '/'),]

執行結果: