程序员最近都爱上了这个网站  程序员们快来瞅瞅吧!  it98k网:it98k.com

本站消息

站长简介/公众号

  出租广告位,需要合作请联系站长

+关注
已关注

分类  

暂无分类

标签  

暂无标签

日期归档  

2023-06(3)

Django——视图——HttpResponse

发布于2020-03-17 20:20     阅读(1648)     评论(0)     点赞(10)     收藏(2)


目录

  1. HttpResponse
  2. 直接返回
  3. 模板返回

 

  1. HttpResponse


  2. 直接返回


    1. def hello(req):
    2. return HttpResponse("hello world")
    3. def goodbye(req):
    4. res = HttpResponse()
    5. res.content = b'good bye'
    6. res.charset = "utf-8"
    7. res.content_type = 'text/html'
    8. return res
    9. def handle_response(request):
    10. # res = HttpResponse("响应对象")
    11. # res.content_type = "text/html"
    12. # res.status_code = 400 # 设置状态码
    13. # return res
    14. # render返回响应对象
    15. # res = render(request,'example.html')
    16. # return res

     

  3. 模板返回


    1. render函数返回,render只是HttpResponse的包装,还是会返回1个HttpResponse对象
    2. def render_to_response(template_name, context=None, content_type=None,
    3. status=None, using=None):
    4. template_name : 模板名称。
    5. context: 1组字典的值添加到模板中。默认情况下,这是1个空的字典。
    6. content_type :MIME类型用于生成文档。
    7. status :为响应状态代码。默认值为200
    8. def studentlist(req):
    9. for key in req.GET.lists():
    10. print(key)
    11. allstudent = Student.objects.all()
    12. return render(req,'studentlist.html',context={'data':allstudent})
    13. 常用方法:
    14. write(content) 设置内容 == obj.content
    15. set_cookie() 设置cookie
    16. delete_cookie() 删除cookie

     

原文链接:https://blog.csdn.net/piduocheng0577/article/details/104887142



所属网站分类: 技术文章 > 博客

作者:我是防疫小可爱

链接:https://www.pythonheidong.com/blog/article/263620/54ffcba6db1c04753be8/

来源:python黑洞网

任何形式的转载都请注明出处,如有侵权 一经发现 必将追究其法律责任

10 0
收藏该文
已收藏

评论内容:(最多支持255个字符)