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/")
    

相关文章:

  • 2022-02-28
  • 2021-08-08
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-08-30
  • 2022-12-23
猜你喜欢
  • 2022-01-15
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
相关资源
相似解决方案