django-HttpResponse,render,redirect

1.导入相应的包

from django.shortcuts import HttpResponse, render, redirect

2.HttpResponse(返回字符串给浏览器)

def index(request):
    # 业务逻辑代码
    return HttpResponse("OK")

3.render(可以将后台的数据传给前端,三个参数:request,url,context)

def index(request):
    # 业务逻辑代码
    context={'username':'xiximayou',}
    return render(request, "index.html", context=context)

3.redirect(跳转到指定的url)

def index(request):
    # 业务逻辑代码
    return redirect("www.baidu.com")

主要还是使用render。

猜你喜欢

转载自www.cnblogs.com/xiximayou/p/11777068.html