django视图获取用户请求信息以及请求头

一. request可以直接在前端用,不需要在render里再定义。

 

request.body  //前端页面数据内容的原生值。如果前端不是从POST,GET,FILES里面获取的数据,就从body里获取

request.POST  //从request.body里拿数据再封装到request.POST里面

request.FILES  //从request.body里拿数据再封装到request.FILES里面

request.GET  //从request.body里拿数据再封装到request.GET里面

request.XXX.getlist  //获取列表

request.post.getlist  //获取列表

 

request.Meta  (request headers)  //获取请求头相关的信息。

request.method  //请求方法

request.path_info  //获取URL

request.COOKIES  //获取cookies

 

二.cookie信息或在rep里添加的键值对将会写到response headers里面

 

a= ‘python’

rep = HttpResponse(…) 或 rep = render(request, …)或rep =redirect(‘/index/’)

a= ‘python’

rep = HttpResponse(a)

rep[‘name’] = ‘zhou’

rep.set_cookie()

return rep

HttpResponse可以返回字节和字符串

 

三.request.environ/request.META

 

{
‘ALLUSERSPROFILE’: ‘C:\\ProgramData’,
‘APPDATA’: ‘C:\\Users\\wenguoli\\AppData\\Roaming’,
‘COMMONPROGRAMFILES’: ‘C:\\Program Files (x86)\\Common Files’,
‘COMMONPROGRAMFILES(X86)’: ‘C:\\Program Files (x86)\\Common Files’,
‘COMMONPROGRAMW6432’: ‘C:\\Program Files\\Common Files’,
‘COMPUTERNAME’: ‘LAPTOP-B96OIQJU’,
‘COMSPEC’: ‘C:\\WINDOWS\\system32\\cmd.exe’,
‘CONFIGSETROOT’: ‘C:\\WINDOWS\\ConfigSetRoot’,
‘FPS_BROWSER_APP_PROFILE_STRING’: ‘Internet Explorer’,
‘FPS_BROWSER_USER_PROFILE_STRING’: ‘Default’,
‘GOPATH’: ‘D:\\software\\GOPATH’,
‘GOROOT’: ‘D:\\software\\go’,
‘HOMEDRIVE’: ‘C:’,
‘HOMEPATH’: ‘\\Users\\wenguoli’,
‘LOCALAPPDATA’: ‘C:\\Users\\wenguoli\\AppData\\Local’,
‘LOGONSERVER’: ‘\\\\LAPTOP-B96OIQJU’,
‘NODE_PATH’: ‘D:\\software\\nodejs\\node_global\\node_modules’,
‘NUMBER_OF_PROCESSORS’: ‘8’,
‘ONEDRIVE’: ‘D:\\OneDrive’,
‘OS’: ‘Windows_NT’,
‘PATH’: ‘D:\\python\\Envs\\django2.0\\Scripts;D:\\software\\python3.6.5\\Scripts\\;D:\\software\\python3.6.5\\;C:\\Program Files (x86)\\Intel\\iCLS Client\\;C:\\Program Files\\Intel\\iCLS Client\\;C:\\Windows\\system32;C:\\Windows;C:\\Windows\\System32\\Wbem;C:\\Windows\\System32\\WindowsPowerShell\\v1.0\\;C:\\Program Files (x86)\\Intel\\Intel(R) Management Engine Components\\DAL;C:\\Program Files\\Intel\\Intel(R) Management Engine Components\\DAL;C:\\Program Files (x86)\\Intel\\Intel(R) Management Engine Components\\IPT;C:\\Program Files\\Intel\\Intel(R) Management Engine Components\\IPT;C:\\Program Files (x86)\\NVIDIA Corporation\\PhysX\\Common;D:\\software\\git\\bin;D:\\software\\git\\Git\\cmd;D:\\software\\nodejs\\;D:\\software\\redis\\Redis-3.2.100;C:\\WINDOWS\\system32;C:\\WINDOWS;C:\\WINDOWS\\System32\\Wbem;C:\\WINDOWS\\System32\\WindowsPowerShell\\v1.0\\;D:\\software\\mingw\\bin;C:\\wamp\\bin\\php\\php5.6.25;D:\\software\\composer;D:\\software\\go\\bin;C:\\Program Files (x86)\\Xoreax\\IncrediBuild;D:\\software\\python3.6.5\\Lib\\site-packages\\Django-1.11.13-py3.6.egg\\django\\bin;C:\\Program Files\\cURL\\bin;D:\\software\\svn\\bin;D:\\software\\cmder\\cmder;D:\\software\\javajdk\\bin;D:\\software\\java\\bin;D:\\software\\GOPATH\\bin;C:\\Users\\wenguoli\\AppData\\Local\\Microsoft\\WindowsApps;D:\\software\\nodejs\\node_global;D:\\software\\FileZilla\\Fiddler;C:\\Users\\wenguoli\\AppData\\Local\\Microsoft\\WindowsApps;C:\\Users\\wenguoli\\AppData\\Roaming\\Composer\\vendor\\bin;C:\\Users\\wenguoli\\AppData\\Local\\atom\\bin;D:\\software\\VSCode\\Microsoft VS Code\\bin’,
‘PATHEXT’: ‘.COM;.EXE;.BAT;.CMD;.VBS;.VBE;.JS;.JSE;.WSF;.WSH;.MSC;.PY;.PYW’,
‘PROCESSOR_ARCHITECTURE’: ‘x86’,
‘PROCESSOR_ARCHITEW6432’: ‘AMD64’,
‘PROCESSOR_IDENTIFIER’: ‘Intel64 Family 6 Model 142 Stepping 10, GenuineIntel’,
‘PROCESSOR_LEVEL’: ‘6’,
‘PROCESSOR_REVISION’: ‘8e0a’,
‘PROGRAMDATA’: ‘C:\\ProgramData’,
‘PROGRAMFILES’: ‘C:\\Program Files (x86)’,
‘PROGRAMFILES(X86)’: ‘C:\\Program Files (x86)’,
‘PROGRAMW6432’: ‘C:\\Program Files’,
‘PROMPT’: ‘(django2.0) $P$G’,
‘PSMODULEPATH’: ‘C:\\Program Files\\WindowsPowerShell\\Modules;C:\\WINDOWS\\system32\\WindowsPowerShell\\v1.0\\Modules’,
‘PUBLIC’: ‘C:\\Users\\Public’,
‘SESSIONNAME’: ‘Console’,
‘SYSTEMDRIVE’: ‘C:’,
‘SYSTEMROOT’: ‘C:\\WINDOWS’,
‘TEMP’: ‘C:\\Users\\wenguoli\\AppData\\Local\\Temp’,
‘TMP’: ‘C:\\Users\\wenguoli\\AppData\\Local\\Temp’,
‘UID’: ‘10103’,
‘USERDOMAIN’: ‘LAPTOP-B96OIQJU’,
‘USERDOMAIN_ROAMINGPROFILE’: ‘LAPTOP-B96OIQJU’,
‘USERNAME’: ‘wenguoli’,
‘USERPROFILE’: ‘C:\\Users\\wenguoli’,
‘VENV’: ‘django2.0’,
‘VIRTUALENVWRAPPER_PROJECT_FILENAME’: ‘.project’,
‘VIRTUAL_ENV’: ‘D:\\python\\Envs\\django2.0’,
‘WINDIR’: ‘C:\\WINDOWS’,
‘WORKON_HOME’: ‘D:\\python\\Envs’,
‘_OLD_VIRTUAL_PATH’: ‘D:\\software\\python3.6.5\\Scripts\\;D:\\software\\python3.6.5\\;C:\\Program Files (x86)\\Intel\\iCLS Client\\;C:\\Program Files\\Intel\\iCLS Client\\;C:\\Windows\\system32;C:\\Windows;C:\\Windows\\System32\\Wbem;C:\\Windows\\System32\\WindowsPowerShell\\v1.0\\;C:\\Program Files (x86)\\Intel\\Intel(R) Management Engine Components\\DAL;C:\\Program Files\\Intel\\Intel(R) Management Engine Components\\DAL;C:\\Program Files (x86)\\Intel\\Intel(R) Management Engine Components\\IPT;C:\\Program Files\\Intel\\Intel(R) Management Engine Components\\IPT;C:\\Program Files (x86)\\NVIDIA Corporation\\PhysX\\Common;D:\\software\\git\\bin;D:\\software\\git\\Git\\cmd;D:\\software\\nodejs\\;D:\\software\\redis\\Redis-3.2.100;C:\\WINDOWS\\system32;C:\\WINDOWS;C:\\WINDOWS\\System32\\Wbem;C:\\WINDOWS\\System32\\WindowsPowerShell\\v1.0\\;D:\\software\\mingw\\bin;C:\\wamp\\bin\\php\\php5.6.25;D:\\software\\composer;D:\\software\\go\\bin;C:\\Program Files (x86)\\Xoreax\\IncrediBuild;D:\\software\\python3.6.5\\Lib\\site-packages\\Django-1.11.13-py3.6.egg\\django\\bin;C:\\Program Files\\cURL\\bin;D:\\software\\svn\\bin;D:\\software\\cmder\\cmder;D:\\software\\javajdk\\bin;D:\\software\\java\\bin;D:\\software\\GOPATH\\bin;C:\\Users\\wenguoli\\AppData\\Local\\Microsoft\\WindowsApps;D:\\software\\nodejs\\node_global;D:\\software\\FileZilla\\Fiddler;C:\\Users\\wenguoli\\AppData\\Local\\Microsoft\\WindowsApps;C:\\Users\\wenguoli\\AppData\\Roaming\\Composer\\vendor\\bin;C:\\Users\\wenguoli\\AppData\\Local\\atom\\bin;D:\\software\\VSCode\\Microsoft VS Code\\bin’,
‘_OLD_VIRTUAL_PROMPT’: ‘$P$G’,
‘DJANGO_SETTINGS_MODULE’: ‘bige.settings’,
‘RUN_MAIN’: ‘true’,
‘SERVER_NAME’: ‘LAPTOP-B96OIQJU’,
‘GATEWAY_INTERFACE’: ‘CGI/1.1’,
‘SERVER_PORT’: ‘8000’,
‘REMOTE_HOST’: ”,
‘CONTENT_LENGTH’: ”,
‘SCRIPT_NAME’: ”,
‘SERVER_PROTOCOL’: ‘HTTP/1.1’,
‘SERVER_SOFTWARE’: ‘WSGIServer/0.2’,
‘REQUEST_METHOD’: ‘GET’,
‘PATH_INFO’: ‘/api/v1/user/7/’,
‘QUERY_STRING’: ”,
‘REMOTE_ADDR’: ‘127.0.0.1’,
‘CONTENT_TYPE’: ‘text/plain’,
‘HTTP_TOKEN’: ‘12121212’,
‘HTTP_CACHE_CONTROL’: ‘no-cache’,
‘HTTP_POSTMAN_TOKEN’: ‘432aad28-fbca-4023-9f89-fad29e737b14’,
‘HTTP_USER_AGENT’: ‘PostmanRuntime/7.1.1’,
‘HTTP_ACCEPT’: ‘*/*’,
‘HTTP_HOST’: ‘127.0.0.1:8000’,
‘HTTP_ACCEPT_ENCODING’: ‘gzip, deflate’,
‘HTTP_CONNECTION’: ‘keep-alive’,
‘wsgi.input’: < _io.BufferedReader name = 1168 > ,
‘wsgi.errors’: < _io.TextIOWrapper name = ‘<stderr>’
mode = ‘w’
encoding = ‘utf-8’ > ,
‘wsgi.version’: (1, 0),
‘wsgi.run_once’: False,
‘wsgi.url_scheme’: ‘http’,
‘wsgi.multithread’: True,
‘wsgi.multiprocess’: False,
‘wsgi.file_wrapper’: < class ‘wsgiref.util.FileWrapper’ >
}