视图相关

视图

1 FBV和CBV

image-20230709143948228

  • 非前后端分离,FBV
  • 前后端分离、小程序API,CBV【django rest framekwork框架)

2 参数

  • request对象,读取请求相关所有的数据。

    request.method
    request.GET        ?name=123&age=123&age=199   =>  特殊的字典:{name:[123],"age":[123,1999]}
    reqeust.POST
    request.FILES
    request.path_info
    request.body
    
    request.resover_math
    request.session
    
  • v1,v2 或 *args,**kwargs,用于接收动态路由中的参数值

3 业务处理

...

4 返回值

from django.http import HttpResponse
from django.http import JsonResponse
from django.shortcuts import render, redirect

# `构建` 响应体和响应头
# return HttpResponse("中国北京")
# return JsonResponse({"status": True, "name": "武沛齐"}, json_dumps_params={"ensure_ascii": False})
# return render(request, "demo.txt")
# return redirect("https://www.baidu.com") # 301/302

问题:那什么时候是301,什么时候是302?

class HttpResponseRedirect(HttpResponseRedirectBase):
    status_code = 302  # 临时重定向

class HttpResponsePermanentRedirect(HttpResponseRedirectBase):
    status_code = 301  # 永久重定向

本文作者:Sherwin

本文链接:https://www.cnblogs.com/sherwin1995/p/18406471

版权声明:本作品采用知识共享署名-非商业性使用-禁止演绎 2.5 中国大陆许可协议进行许可。

posted @   Sherwin_szw  阅读(3)  评论(0编辑  收藏  举报
点击右上角即可分享
微信分享提示
💬
评论
📌
收藏
💗
关注
👍
推荐
🚀
回顶
收起
  1. 1 404 not found REOL
404 not found - REOL
00:00 / 00:00
An audio error has occurred.