【转】django 三件套(render,redirect,HttpResponse)

Django基础必备三件套**:

  • HttpResponse 内部传入一个字符串参数,返回给浏览器。

from django.shortcuts import HttpResponse
def index(request):
    # 业务逻辑代码
    return HttpResponse("OK")
  • render 除request参数外还接受一个待渲染的模板文件和一个保存具体数据的字典参数。

    将数据填充进模板文件,最后把结果返回给浏览器。   

from django.shortcuts import render
def index(request):
    # 业务逻辑代码
    return render(request, "index.html", {"name": "alex", "hobby": ["烫头", "泡吧"]})
  • redirect 接受一个URL参数,表示跳转到指定的URL。
from django.shortcuts import redirect
def index(request):
    # 业务逻辑代码
    return redirect("/home/")

猜你喜欢

转载自www.cnblogs.com/HYanqing/p/11615592.html
今日推荐