zoukankan      html  css  js  c++  java
  • 【转】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/")
  • 相关阅读:
    Vijos1986
    vijos1790
    洛谷1005
    洛谷3381
    bzoj4034
    bzoj1564
    bzoj2648
    洛谷3348
    jzoi4964
    codevs3990
  • 原文地址:https://www.cnblogs.com/HYanqing/p/11615592.html
Copyright © 2011-2022 走看看