Django模板例子

2021-06-20 01:41:04 字數 686 閱讀 7518

使用django開發第乙個render例子。

首先準備python開發環境然後建立相應的專案 

(1)django-admin.py startproject hello 

(3)設定django的template路徑,在hello目錄下的setting.py中設定:

template_dirs = (

'f:/hello/blog/index',注意windows中要這樣設定為正斜槓

)

(4)新增下面的路徑對映到urls.py 

url(r'^blog/index/$', 'blog.views.hello'),
(5) 在blog下的views.py中新增hello方法和rende

return render(req, 'index.html', )在index.html中任意位置設定:  ,即可在該位置顯示bar.。。。。感覺跟php的smarty模板很像,不過模板也一般都是這個原理了。。找出相應的變數進行替換。。

一切ok現在可以訪問頁面。。。 

Django教程 Django模板

如何向請求者返回乙個漂亮的頁面呢?肯定需要用到html css,如果想要更炫的效果還要加入js,問題來了,這麼一堆字串全都寫到檢視中,作為httpresponse 的引數嗎?先看看如何拼接html 圖書首頁 if request.method get res html books 射鵰英雄傳 神鵰俠...

例子與模板

在軟體領域中,例子對於學習新知識新的技能非常重要,不管多麼詳盡的講解都不如乙個活生生的例子更讓人容易理解某個知識,這一點完全可以應用於教學中,寫作中等傳授性的工作中,同時要學習乙個新的知識的第一步應該是尋找例子,從例子中尋找答案。模板 同樣具有極其重要的作用 將某些通用的東西提出出來,形成模板,日積...

Django 模板使用

環境 python 2.7 32位,django1.6.7,win7 64位系統 模板載入 在mysite下新建乙個templates資料夾,然後在setting.py新增以下內容 import os.path template dirs os.path.join os.path.dirname f...