render()
此方法的作用---結合一個給定的模板和一個給定的上下文字典,並返回一個渲染后的 HttpResponse 對象。
通俗的講就是把context的內容, 加載進templates中定義的文件, 並通過瀏覽器渲染呈現.
簡單示例:
hello.html
<h1>{{hello }}</h1>
#views.py from django.shortcuts import render def hello(request): context = {} context['hello'] = 'Hello World!' return render(request, 'hello.html', context) #return render(request, 'hello.html', {'hello':'Hello World!'})
render()函數傳遞context來填充模板
help文檔中render描述
render(request, template_name, context=None, content_type=None, status=None, using=None)
參數:
request: 是一個固定參數
template_name: templates中定義的文件,注意路徑名。比如:"templates/polls/index.html", 則參數這樣寫:"polls/index.html"
context: 要傳入文件中用於渲染呈現的數據, 默認是字典格式
content_type: 生成的文檔要使用的MIME 類型。默認為DEFAULT_CONTENT_TYPE 設置的值。
status: http的響應代碼,默認是200.
using: 用於加載模板使用的模板引擎的名稱。