首先導入它們:
from django.shortcuts import HttpResponse, render, redirect
1.HttpResponse
它是作用是內部傳入一個字符串參數,然后發給瀏覽器。
例如:
def index(request): # 業務邏輯代碼 return HttpResponse("OK")
2、render
render方法可接收三個參數,一是request參數,二是待渲染的html模板文件,三是保存具體數據的字典參數。
它的作用就是將數據填充進模板文件,最后把結果返回給瀏覽器。與jinja2類似。
例如:
def index(request): # 業務邏輯代碼 return render(request, "index.html", {"name": "hello", "key": ["reading", "www"]})
3、redirect
接受一個URL參數,表示讓瀏覽器跳轉去指定的URL.
例如:
def index(request): # 業務邏輯代碼 return redirect("https://www.baidu.com/")