Django render函數


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: 用於加載模板使用的模板引擎的名稱。

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM