• 主页

  • 投资

  • IT

    🔥
  • 设计

  • 销售

  • 共43篇

    python3.X - Web - Django3.2.9

关闭

返回栏目

关闭

返回python3.X - Web - Django3.2.9栏目

15 - 请求 - views.py - 获得数据

作者:

贺及楼

成为作者

更新日期:2024-03-16 11:14:01

Django获得数据
  1. 属性介绍
  2. request.META
  3. request.scheme
  4. request.path
  5. request.encoding
  6. request.session
  7. request.COOKIES
  8. request.method
  9. 获取数据的方法
  10. request.body
  11. request.data
  12. name = request.GET.get("name")
  13. name = request.POST.get("name")
  14. request.FILES.get('pic')
  15. FILES 只有在请求的方法为POST 且提交的<form> 带有enctype="multipart/form-data" 的情况下才会包含数据。否则,FILES 将为一个空的类似于字典的对象
  1. def ttttt(request):
  2. # 获取上传文件的处理对象
  3. try:
  4. pic = request.FILES.get('pic')
  5. method = request.method
  6. print(method) # POST GET
  7. body = request.body
  8. print(body)
  9. path = request.path # http://127.0.0.1:8000/cockpit/ttttt/?AA=aa&BB=bb
  10. print(path) # /cockpit/ttttt/
  11. full_path = request.get_full_path()
  12. print(full_path) # /cockpit/ttttt/?AA=aa&BB=bb
  13. ajax = request.is_ajax() # 判断是不是ajax发送的请求
  14. print(ajax) # False
  15. cookies = request.COOKIES
  16. print(cookies) # {'cookie1': '11', 'cookie2': '22'}
  17. cookie1 = request.COOKIES.get("cookie1")
  18. print(cookie1) # '11'
  19. meta = request.META
  20. # print(meta) # 好多好多参数,看下方
  21. get = request.GET
  22. print(get) # <QueryDict: {'AA': ['aa'], 'BB': ['bb']}>
  23. AA = request.GET.get("AA", None) # http://127.0.0.1:8000/cockpit/ttttt/?AA=aa&BB=bb 不管get请求还是post请求都可以拿到
  24. print(AA) # aa
  25. CC = request.POST.get("aas", None)
  26. print(CC) # cc
  27. return JsonResponse({"code": 20000, 'message': '删除成功'})
  28. except Exception as e:
  29. return JsonResponse({"code": 20001, 'error': str(e)})

request.META

  1. # {
  2. # "ALLUSERSPROFILE": "C:\\ProgramData",
  3. # "APPDATA": "C:\\Users\\admin123\\AppData\\Roaming",
  4. # "BKAPP_MULTIPROCESSING": "false",
  5. # "CHROME_CRASHPAD_PIPE_NAME": "\\\\.\\pipe\\crashpad_8836_WGDPSHPFXZAWCXBB",
  6. # "COMMONPROGRAMFILES": "C:\\Program Files\\Common Files",
  7. # "COMMONPROGRAMFILES(X86)": "C:\\Program Files (x86)\\Common Files",
  8. # "COMMONPROGRAMW6432": "C:\\Program Files\\Common Files",
  9. # "COMPUTERNAME": "DESKTOP-ES61745",
  10. # "COMSPEC": "C:\\windows\\system32\\cmd.exe",
  11. # "DRIVERDATA": "C:\\Windows\\System32\\Drivers\\DriverData",
  12. # "ERLANG_HOME": "C:\\Program Files\\Erlang OTP",
  13. # "FPS_BROWSER_APP_PROFILE_STRING": "Internet Explorer",
  14. # "FPS_BROWSER_USER_PROFILE_STRING": "Default",
  15. # "HOMEDRIVE": "C:",
  16. # "HOMEPATH": "\\Users\\admin123",
  17. # "IGCCSVC_DB": "AQAAANCMndERjHoAwE/Cl+sBAAAA0GpR0b7Jy0yURbPMJgTTcgQAAAAAAAAQZgAAAAEAAAAADXXtj5NyqQ0Sqv9XjEJgwY6xRRCiM34kLOZzBNN3m6GQAAAAAOgAAAAAIAACAAAACz24uPAbKhh6nhFJKaAu+mlCPnCyTJLReqNN+RCszDlAwmaucg9Mjqpcn6TKFwLZZeqiDZrcACnTI3013TPuCknY2L0G3h5/8CrByOvKhMTjyX0/QpuNuh1VHGD/+9AAAAAqBcM0pH/G96h7mliya9bDed6OYaZYkkuyu3WGT1mIrfVtB8JzYYcJ8xHYzkAvuzbQ2KWXyOChFvwCyrEyeTVJA==",
  18. # "JAVA_HOME": "C:\\Program Files (x86)\\AdoptOpenJDK",
  19. # "LOCALAPPDATA": "C:\\Users\\admin123\\AppData\\Local",
  20. # "LOGONSERVER": "\\\\DESKTOP-ES61745",
  21. # "MVN_HOME": "C:\\Users\\admin123\\Desktop\\tjhb\\mvn\\apache-maven-3.8.5\\bin",
  22. # "NUMBER_OF_PROCESSORS": "8",
  23. # "NVM_HOME": "C:\\Users\\admin123\\AppData\\Roaming\\nvm",
  24. # "NVM_SYMLINK": "C:\\Program Files\\nodejs",
  25. # "ONEDRIVE": "C:\\Users\\admin123\\OneDrive",
  26. # "ONEDRIVECONSUMER": "C:\\Users\\admin123\\OneDrive",
  27. # "ONLINESERVICES": "Online Services",
  28. # "ORIGINAL_XDG_CURRENT_DESKTOP": "undefined",
  29. # "OS": "Windows_NT",
  30. # "PATH": "C:\\Users\\admin123\\Desktop\\tjhb\\huabao_screen\\huabao-screen-server\\venv\\Scripts;C:\\ProgramData\\Oracle\\Java\\javapath;C:\\Python27\\;C:\\Python27\\Scripts\\;C:\\Program Files (x86)\\AdoptOpenJDK\\jdk-8.0.292.10-openj9\\bin;C:\\windows\\system32;C:\\windows;C:\\windows\\System32\\Wbem;C:\\windows\\System32\\WindowsPowerShell\\v1.0\\;C:\\windows\\System32\\OpenSSH\\;C:\\Program Files (x86)\\NVIDIA Corporation\\PhysX\\Common;C:\\Program Files\\Bandizip\\;C:\\Program Files\\TortoiseSVN\\bin;C:\\Program Files (x86)\\Windows Kits\\8.1\\Windows Performance Toolkit\\;C:\\Program Files (x86)\\AdoptOpenJDK\\bin;C:\\Program Files (x86)\\AdoptOpenJDK\\jre\\bin;C:\\Program Files (x86)\\PuTTY\\;C:\\Program Files\\Git\\cmd;C:\\Users\\admin123\\AppData\\Roaming\\nvm;C:\\Program Files\\nodejs;C:\\Program Files\\AsiaInfo Security\\AisEsmAgent;C:\\Program Files\\Redis\\;C:\\Program Files\\Erlang OTP\\bin;C:\\Program Files\\RabbitMQ Server\\rabbitmq_server-3.12.12\\sbin;C:\\Python36\\Scripts\\;C:\\Python36\\;C:\\Users\\admin123\\AppData\\Local\\Microsoft\\WindowsApps;C:\\Users\\admin123\\Desktop\\tjhb\\mvn\\apache-maven-3.8.5\\bin;C:\\Users\\admin123\\AppData\\Local\\Programs\\Microsoft VS Code\\bin;C:\\Python27\\;C:\\Program Files\\mysql-5.7.38-winx64\\mysql-5.7.38-winx64\\bin;C:\\Python27\\Scripts\\;C:\\Users\\admin123\\AppData\\Roaming\\nvm\\v16.13.0;C:\\Users\\admin123\\AppData\\Roaming\\nvm;C:\\Program Files\\nodejs;C:\\Users\\admin123\\AppData\\Local\\Programs\\Python\\Python310\\;C:\\Users\\admin123\\AppData\\Local\\Programs\\Python\\Python310\\Scripts\\;C:\\Program Files\\MongoDB\\Server\\6.0\\bin;C:\\Program Files\\mysql-5.7.38-winx64\\mysql-5.7.38-winx64\\include;",
  31. # "PATHEXT": ".COM;.EXE;.BAT;.CMD;.VBS;.VBE;.JS;.JSE;.WSF;.WSH;.MSC;.CPL",
  32. # "PLATFORMCODE": "AN",
  33. # "PROCESSOR_ARCHITECTURE": "AMD64",
  34. # "PROCESSOR_IDENTIFIER": "Intel64 Family 6 Model 140 Stepping 1, GenuineIntel",
  35. # "PROCESSOR_LEVEL": "6",
  36. # "PROCESSOR_REVISION": "8c01",
  37. # "PROGRAMDATA": "C:\\ProgramData",
  38. # "PROGRAMFILES": "C:\\Program Files",
  39. # "PROGRAMFILES(X86)": "C:\\Program Files (x86)",
  40. # "PROGRAMW6432": "C:\\Program Files",
  41. # "PSMODULEPATH": "C:\\Users\\admin123\\Documents\\WindowsPowerShell\\Modules;C:\\Program Files\\WindowsPowerShell\\Modules;C:\\windows\\system32\\WindowsPowerShell\\v1.0\\Modules",
  42. # "PUBLIC": "C:\\Users\\Public",
  43. # "PYTHONUNBUFFERED": "1",
  44. # "RABBITMQ_SERVER": "C:\\Program Files\\RabbitMQ Server\\rabbitmq_server-3.12.12",
  45. # "REGIONCODE": "APJ",
  46. # "SESSIONNAME": "Console",
  47. # "SSO_CLIENT_DIRECTORY": "c:\\sso_client\\",
  48. # "SVN_EXPERIMENTAL_COMMANDS": "shelf2",
  49. # "SYSTEMDRIVE": "C:",
  50. # "SYSTEMROOT": "C:\\windows",
  51. # "TEMP": "C:\\Users\\admin123\\AppData\\Local\\Temp",
  52. # "TMP": "C:\\Users\\admin123\\AppData\\Local\\Temp",
  53. # "USERDOMAIN": "DESKTOP-ES61745",
  54. # "USERDOMAIN_ROAMINGPROFILE": "DESKTOP-ES61745",
  55. # "USERNAME": "admin123",
  56. # "USERPROFILE": "C:\\Users\\admin123",
  57. # "VBOX_MSI_INSTALL_PATH": "C:\\Program Files\\Oracle\\VirtualBox\\",
  58. # "VIRTUAL_ENV": "C:\\Users\\admin123\\Desktop\\tjhb\\huabao_screen\\huabao-screen-server\\venv",
  59. # "VS140COMNTOOLS": "C:\\Program Files (x86)\\Microsoft Visual Studio 14.0\\Common7\\Tools\\",
  60. # "WINDIR": "C:\\windows",
  61. # "ZES_ENABLE_SYSMAN": "1",
  62. # "TERM_PROGRAM": "vscode",
  63. # "TERM_PROGRAM_VERSION": "1.86.2",
  64. # "LANG": "en_US.UTF-8",
  65. # "COLORTERM": "truecolor",
  66. # "GIT_ASKPASS": "c:\\Users\\admin123\\AppData\\Local\\Programs\\Microsoft VS Code\\resources\\app\\extensions\\git\\dist\\askpass.sh",
  67. # "VSCODE_GIT_ASKPASS_NODE": "C:\\Users\\admin123\\AppData\\Local\\Programs\\Microsoft VS Code\\Code.exe",
  68. # "VSCODE_GIT_ASKPASS_EXTRA_ARGS": "",
  69. # "VSCODE_GIT_ASKPASS_MAIN": "c:\\Users\\admin123\\AppData\\Local\\Programs\\Microsoft VS Code\\resources\\app\\extensions\\git\\dist\\askpass-main.js",
  70. # "VSCODE_GIT_IPC_HANDLE": "\\\\.\\pipe\\vscode-git-adce85326b-sock",
  71. # "VSCODE_INJECTION": "1",
  72. # "_OLD_VIRTUAL_PATH": "C:\\ProgramData\\Oracle\\Java\\javapath;C:\\Python27\\;C:\\Python27\\Scripts\\;C:\\Program Files (x86)\\AdoptOpenJDK\\jdk-8.0.292.10-openj9\\bin;C:\\windows\\system32;C:\\windows;C:\\windows\\System32\\Wbem;C:\\windows\\System32\\WindowsPowerShell\\v1.0\\;C:\\windows\\System32\\OpenSSH\\;C:\\Program Files (x86)\\NVIDIA Corporation\\PhysX\\Common;C:\\Program Files\\Bandizip\\;C:\\Program Files\\TortoiseSVN\\bin;C:\\Program Files (x86)\\Windows Kits\\8.1\\Windows Performance Toolkit\\;C:\\Program Files (x86)\\AdoptOpenJDK\\bin;C:\\Program Files (x86)\\AdoptOpenJDK\\jre\\bin;C:\\Program Files (x86)\\PuTTY\\;C:\\Program Files\\Git\\cmd;C:\\Users\\admin123\\AppData\\Roaming\\nvm;C:\\Program Files\\nodejs;C:\\Program Files\\AsiaInfo Security\\AisEsmAgent;C:\\Program Files\\Redis\\;C:\\Program Files\\Erlang OTP\\bin;C:\\Program Files\\RabbitMQ Server\\rabbitmq_server-3.12.12\\sbin;C:\\Python36\\Scripts\\;C:\\Python36\\;C:\\Users\\admin123\\AppData\\Local\\Microsoft\\WindowsApps;C:\\Users\\admin123\\Desktop\\tjhb\\mvn\\apache-maven-3.8.5\\bin;C:\\Users\\admin123\\AppData\\Local\\Programs\\Microsoft VS Code\\bin;C:\\Python27\\;C:\\Program Files\\mysql-5.7.38-winx64\\mysql-5.7.38-winx64\\bin;C:\\Python27\\Scripts\\;C:\\Users\\admin123\\AppData\\Roaming\\nvm\\v16.13.0;C:\\Users\\admin123\\AppData\\Roaming\\nvm;C:\\Program Files\\nodejs;C:\\Users\\admin123\\AppData\\Local\\Programs\\Python\\Python310\\;C:\\Users\\admin123\\AppData\\Local\\Programs\\Python\\Python310\\Scripts\\;C:\\Program Files\\MongoDB\\Server\\6.0\\bin;C:\\Program Files\\mysql-5.7.38-winx64\\mysql-5.7.38-winx64\\include;",
  73. # "DJANGO_SETTINGS_MODULE": "settings",
  74. # "RUN_MAIN": "true",
  75. # "SERVER_NAME": "activate.navicat.com",
  76. # "GATEWAY_INTERFACE": "CGI/1.1",
  77. # "SERVER_PORT": "8000",
  78. # "REMOTE_HOST": "",
  79. # "CONTENT_LENGTH": "20",
  80. # "SCRIPT_NAME": "",
  81. # "SERVER_PROTOCOL": "HTTP/1.1",
  82. # "SERVER_SOFTWARE": "WSGIServer/0.2",
  83. # "REQUEST_METHOD": "POST",
  84. # "PATH_INFO": "/cockpit/ttttt/",
  85. # "QUERY_STRING": "AA=aa&BB=bb",
  86. # "REMOTE_ADDR": "127.0.0.1",
  87. # "CONTENT_TYPE": "application/json",
  88. # "HTTP_COOKIE": "cookie1=11;cookie2=22",
  89. # "HTTP_USER_AGENT": "PostmanRuntime/7.36.3",
  90. # "HTTP_ACCEPT": "*/*",
  91. # "HTTP_CACHE_CONTROL": "no-cache",
  92. # "HTTP_POSTMAN_TOKEN": "7fc1e5ed-f218-4d00-af4d-a2152ad30740",
  93. # "HTTP_HOST": "127.0.0.1:8000",
  94. # "HTTP_ACCEPT_ENCODING": "gzip, deflate, br",
  95. # "HTTP_CONNECTION": "keep-alive",
  96. # "wsgi.input": <django.core.handlers.wsgi.LimitedStream object at 0x00000133CB0F6748>,
  97. # "wsgi.errors": <_io.TextIOWrapper name="<stderr>" mode="w" encoding="utf-8">,
  98. # "wsgi.version": (1,
  99. # 0),
  100. # "wsgi.run_once": False,
  101. # "wsgi.url_scheme": "http",
  102. # "wsgi.multithread":
  103. # True,
  104. # "wsgi.multiprocess": False,
  105. # "wsgi.file_wrapper": <class "wsgiref.util.FileWrapper">
  106. # }
  107. # CONTENT_LENGTH —— 请求的正文的长度(是一个字符串)。
  108. # CONTENT_TYPE —— 请求的正文的MIME 类型。
  109. # HTTP_ACCEPT —— 响应可接收的Content-Type。
  110. # HTTP_ACCEPT_ENCODING —— 响应可接收的编码。
  111. # HTTP_ACCEPT_LANGUAGE —— 响应可接收的语言。
  112. # HTTP_HOST —— 客服端发送的HTTP Host 头部。
  113. # HTTP_REFERER —— Referring 页面。
  114. # HTTP_USER_AGENT —— 客户端的user-agent 字符串。
  115. # QUERY_STRING —— 单个字符串形式的查询字符串(未解析过的形式)。
  116. # REMOTE_ADDR —— 客户端的IP 地址。
  117. # REMOTE_HOST —— 客户端的主机名。
  118. # REMOTE_USER —— 服务器认证后的用户。
  119. # REQUEST_METHOD —— 一个字符串,例如"GET" 或"POST"。
  120. # SERVER_NAME —— 服务器的主机名。
  121. # SERVER_PORT —— 服务器的端口(是一个字符串)。