1. 創建Templates資料夾於專案目錄底下
更改settings.py讓Django找到Templates的路徑
2. 賦予一個變數templates資料夾路徑
TEMPLATE_DIR = os.path.join(BASE_DIR, 'templates').replace('\\', '/')
找到TEMPLATE = []中的 DIRS 並貼上變數TEMPLATE_DIR
連接templates資料夾完成
3. 新增模板於templates,如index.html,內容加上模板標籤
# index.html
<h1>Hello index</h1>
{{ insert_me }} # 模板變數
4. 在views的function中,使用render()選擇index.html並傳參數給模板
def index(request):
my_dict = {'insert_me':'Hello I am from views.py'}
return render(request, 'index.html', context = my_dict)
完成
沒有留言:
張貼留言