目录
-
HttpResponse
-
直接返回
def hello(req): return HttpResponse("hello world") def goodbye(req): res = HttpResponse() res.content = b'good bye' res.charset = "utf-8" res.content_type = 'text/html' return res def handle_response(request): # res = HttpResponse("响应对象") # res.content_type = "text/html" # res.status_code = 400 # 设置状态码 # return res # render返回响应对象 # res = render(request,'example.html') # return res
-
模板返回
render函数返回,render只是HttpResponse的包装,还是会返回1个HttpResponse对象 def render_to_response(template_name, context=None, content_type=None, status=None, using=None): template_name : 模板名称。 context: 1组字典的值添加到模板中。默认情况下,这是1个空的字典。 content_type :MIME类型用于生成文档。 status :为响应状态代码。默认值为200 def studentlist(req): for key in req.GET.lists(): print(key) allstudent = Student.objects.all() return render(req,'studentlist.html',context={'data':allstudent}) 常用方法: write(content) 设置内容 == obj.content set_cookie() 设置cookie delete_cookie() 删除cookie