django基礎入門(3)django中模板

2021-09-06 09:08:35 字數 1288 閱讀 3593

上一節中的輸入,即檢視中的return httpresponse()部分。函式中的內容為……

意思就是,前端檔案,要每次都要手寫,列印,這非常麻煩。通常,它會包括很多內容,還有js

檔案,css

檔案等。而且設計頁面也不好設計,或者設計好了,再貼上

html

字串,進行輸出。且會發現

html

**與python

後台**摻雜在一起。

1 通過自定義,

直接在檢視中寫。新建立檢視(函式)hello_who

如下:

通過建立模板,輸出hello

,who

這裡who

的名字為中文的宋江。,如果在頁面中正常顯示中文,需要在

settings.py

中設定language_code = 'zh-cn'

且在視**件views.py中,第一行加入#encoding=utf-8

2 使用模板

先建立模板檔案,檔名任意。模板檔案就是html

前台檔案。與

aspx

頁類似。先建立乙個

hello.html

檔案。如下:

hello,}

然後在檢視中載入這個模板,並給變數賦值。

檢視hello_who

改為:

其中,獲得模板,和呈現函式,可以利用乙個函式實現,如下:

def hello_who(request):

return render_to_response('hello.html', )

另使用這個函式,需要引入:

from django.shortcuts import render_to_response

通過locals()

返回所有定義的對映

def hello_who(request):

name= '宋江'

return render_to_response('hello.html', locals())

django入門 基礎命令

1.新建乙個 django project django admin startproject project name 特別是在 windows 上,如果報錯,嘗試用 django admin 代替 django admin.py 試試 注意 project name 是自己的專案名稱,需要為合法...

Django入門,很基礎的

如果這是你第一次使用django,你必須注意一些初始化過程 執行 django admin.py startproject mysite 將會在你的當前目錄下建立乙個mysite目錄 注意,如果你使用setup.py安裝django,django admin.py應該在你的path系統變數下 如果不...

Django基礎3(Django框架介紹)

mvc,全名是model view controller,是軟體工程中的一種軟體架構模式,把軟體系統分為三個基本部分 模型 model 檢視 view 和控制器 controller 具有耦合性低 重用性高 生命週期成本低等優點。django框架的設計模式借鑑了mvc框架的思想,也是分成三部分,來降...