Django render函數

render()html

此方法的做用---結合一個給定的模板和一個給定的上下文字典,並返回一個渲染後的 HttpResponse 對象。django

通俗的講就是把context的內容, 加載進templates中定義的文件, 並經過瀏覽器渲染呈現.瀏覽器

簡單示例:函數

hello.htmlspa

<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來填充模板code

 

help文檔中render描述htm

render(request, template_name, context=None, content_type=None, status=None, using=None)
對象

參數:blog

request: 是一個固定參數模板引擎

template_name: templates中定義的文件,注意路徑名。好比:"templates/polls/index.html", 則參數這樣寫:"polls/index.html"

context: 要傳入文件中用於渲染呈現的數據, 默認是字典格式

content_type: 生成的文檔要使用的MIME 類型。默認爲DEFAULT_CONTENT_TYPE 設置的值。

status: http的響應代碼,默認是200.

using: 用於加載模板使用的模板引擎的名稱。

相關文章
相關標籤/搜索