Django 如何获取前端发送的头文件详解(推荐)

雪幕拉开,飘然而至分外美妙,初起便显现出意境;雪落十分,然间有种喜悦在心间荡漾,纷飞飘扬尽享每一刻青春的激情;飞舞的雪花,感受芬芳的韵味,迷人心脾围绕在身旁;耳畔,时常想有沙沙的声音那样的动听,百听不厌,着迷在这一刻升起动人心怀的旋律。

Django里面,我们知道可以在函数里面里面通过参数获取前端发来的数据。
比如可以通过request获取各种form的值啊,cookie啊等等。

def index(reqeust):
 # 获取当前已经登录的用户
 v = reqeust.COOKIES.get('username111')
 return render(reqeust,'index.html',{'current_user': v})

这些方法是Django把一些常用的数据已经替我们封装好了,因此可以直接使用。但是这些方法并不全,比如我刷新一个页面,这个请求的头文件里面,包含了一堆关于客户端的信息,如何获取所有的信息呢?

我们可以通过request.environ来获取

首先需要导入一个类,如果忘记了这个类的名字,可以直接print(type(request))来获取。

<class 'django.core.handlers.wsgi.WSGIRequest'>

environ本质是个字典格式,因此可以直接print(k,v)获取所有的key和value,也可以通过对应的key来输出value

def tpl1(request):
 
 from django.core.handlers.wsgi import WSGIRequest
 print(type(request))
 print(request.environ)
 for k,v in request.environ.items():
 print(k,v)
 print(request.environ['HTTP_USER_AGENT'])
 user_list = [1, 2, 3, 43]
 return render(request, 'tpl1.html', {'u': user_list})

输出结果:

{'ASL.LOG': 'Destination=file', 'PYTHONIOENCODING': 'UTF-8', 'wsgi.run_once': False, 'FPS_BROWSER_APP_PROFILE_STRING': 'Internet Explorer', 'REMOTE_ADDR': '127.0.0.1', 'PROCESSOR_IDENTIFIER': 'Intel64 Family 6 Model 69 Stepping 1, GenuineIntel', 'TEMP': 'C:\\Users\\yuan.li\\AppData\\Local\\Temp', 'USERPROFILE': 'C:\\Users\\yuan.li', 'SERVER_PORT': '8000', 'WINDIR': 'C:\\WINDOWS', 'SYSTEMDRIVE': 'C:', 'FP_NO_HOST_CHECK': 'NO', 'COMMONPROGRAMFILES(X86)': 'C:\\Program Files (x86)\\Common Files', 'USERDNSDOMAIN': 'OMNICOM.COM.AU', 'HTTP_UPGRADE_INSECURE_REQUESTS': '1', 'PROGRAMFILES': 'C:\\Program Files (x86)', 'USERDOMAIN_ROAMINGPROFILE': 'OMNICOM', 'wsgi.url_scheme': 'http', 'wsgi.input': <_io.BufferedReader name=760>, 'APPDATA': 'C:\\Users\\yuan.li\\AppData\\Roaming', 'TMP': 'C:\\Users\\yuan.li\\AppData\\Local\\Temp', 'HTTP_ACCEPT_ENCODING': 'gzip, deflate, br', 'GATEWAY_INTERFACE': 'CGI/1.1', 'PATH_INFO': '/tpl1/', 'PUBLIC': 'C:\\Users\\Public', 'HTTP_COOKIE': 'csrftoken=eIEjNdtcPYmGD2jGDfEwxb0MwePMQTuLWchxG85P7zH7wHN2BZmERsppA4PHGd6D', 'PROCESSOR_ARCHITEW6432': 'AMD64', 'COMPUTERNAME': 'YLI-PC', 'wsgi.version': (1, 0), 'ONEDRIVE': 'C:\\Users\\yuan.li\\OneDrive', 'CONTENT_TYPE': 'text/plain', 'PROGRAMW6432': 'C:\\Program Files', 'PYTHONUNBUFFERED': '1', 'wsgi.errors': <_io.TextIOWrapper name='<stderr>' mode='w' encoding='UTF-8'>, 'RUN_MAIN': 'true', 'PSMODULEPATH': 'C:\\WINDOWS\\system32\\WindowsPowerShell\\v1.0\\Modules\\;C:\\Program Files (x86)\\VMware\\Infrastructure\\vSphere PowerCLI\\Modules', 'wsgi.file_wrapper': <class 'wsgiref.util.FileWrapper'>, 'HOMEPATH': '\\Users\\yuan.li', 'COMSPEC': 'C:\\WINDOWS\\system32\\cmd.exe', 'USERNAME': 'yuan.li', 'PATHEXT': '.COM;.EXE;.BAT;.CMD;.VBS;.VBE;.JS;.JSE;.WSF;.WSH;.MSC;.PY;.PYW', 'SERVER_NAME': 'vmware-localhost', 'USERDOMAIN': 'OMNICOM', 'SCRIPT_NAME': '', 'PYCHARM_HOSTED': '1', 'QUERY_STRING': '', 'PROGRAMDATA': 'C:\\ProgramData', 'NUMBER_OF_PROCESSORS': '4', 'FPS_BROWSER_USER_PROFILE_STRING': 'Default', 'SERVER_PROTOCOL': 'HTTP/1.1', 'HTTP_CONNECTION': 'keep-alive', 'REMOTE_HOST': '', 'HTTP_ACCEPT_LANGUAGE': 'en-GB,en-US;q=0.8,en;q=0.6', 'HTTP_CACHE_CONTROL': 'max-age=0', 'HTTP_ACCEPT': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8', 'wsgi.multiprocess': False, 'ALLUSERSPROFILE': 'C:\\ProgramData', 'SYSTEMROOT': 'C:\\WINDOWS', 'SERVER_SOFTWARE': 'WSGIServer/0.2', 'PROCESSOR_REVISION': '4501', 'SESSIONNAME': 'Console', 'HTTP_HOST': '127.0.0.1:8000', 'CONTENT_LENGTH': '', 'PATH': 'C:\\Program Files (x86)\\Business Objects\\Common\\3.5\\bin\\NOTES\\;C:\\Program Files (x86)\\Business Objects\\Common\\3.5\\bin\\NOTES\\DATA\\;C:\\Python35-32\\Scripts\\;C:\\Python35-32\\;C:\\ProgramData\\Oracle\\Java\\javapath;C:\\WINDOWS\\system32;C:\\WINDOWS;C:\\WINDOWS\\System32\\Wbem;C:\\WINDOWS\\System32\\WindowsPowerShell\\v1.0\\;C:\\Program Files (x86)\\Sennheiser\\SoftphoneSDK\\;C:\\Python35-32\\Scripts;C:\\Users\\yuan.li\\AppData\\Local\\Microsoft\\WindowsApps;C:\\Program Files (x86)\\Nmap;', 'OS': 'Windows_NT', 'PYTHONPATH': 'C:\\s14day21', 'HOMEDRIVE': 'C:', 'COMMONPROGRAMW6432': 'C:\\Program Files\\Common Files', 'DJANGO_SETTINGS_MODULE': 's14day21.settings', 'PROCESSOR_ARCHITECTURE': 'x86', 'REQUEST_METHOD': 'GET', 'LOCALAPPDATA': 'C:\\Users\\yuan.li\\AppData\\Local', 'PROCESSOR_LEVEL': '6', 'HTTP_USER_AGENT': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/59.0.3071.115 Safari/537.36', 'PROGRAMFILES(X86)': 'C:\\Program Files (x86)', 'LOGONSERVER': '\\\\MELDC01', 'wsgi.multithread': True, 'COMMONPROGRAMFILES': 'C:\\Program Files (x86)\\Common Files'}
ASL.LOG Destination=file
PYTHONIOENCODING UTF-8
wsgi.run_once False
FPS_BROWSER_APP_PROFILE_STRING Internet Explorer
REMOTE_ADDR 127.0.0.1
PROCESSOR_IDENTIFIER Intel64 Family 6 Model 69 Stepping 1, GenuineIntel
TEMP C:\Users\yuan.li\AppData\Local\Temp
USERPROFILE C:\Users\yuan.li
SERVER_PORT 8000
WINDIR C:\WINDOWS
SYSTEMDRIVE C:
FP_NO_HOST_CHECK NO
COMMONPROGRAMFILES(X86) C:\Program Files (x86)\Common Files
USERDNSDOMAIN OMNICOM.COM.AU
HTTP_UPGRADE_INSECURE_REQUESTS 1
PROGRAMFILES C:\Program Files (x86)
USERDOMAIN_ROAMINGPROFILE OMNICOM
wsgi.url_scheme http
wsgi.input <_io.BufferedReader name=760>
APPDATA C:\Users\yuan.li\AppData\Roaming
TMP C:\Users\yuan.li\AppData\Local\Temp
HTTP_ACCEPT_ENCODING gzip, deflate, br
GATEWAY_INTERFACE CGI/1.1
PATH_INFO /tpl1/
PUBLIC C:\Users\Public
HTTP_COOKIE csrftoken=eIEjNdtcPYmGD2jGDfEwxb0MwePMQTuLWchxG85P7zH7wHN2BZmERsppA4PHGd6D
PROCESSOR_ARCHITEW6432 AMD64
COMPUTERNAME YLI-PC
wsgi.version (1, 0)
ONEDRIVE C:\Users\yuan.li\OneDrive
CONTENT_TYPE text/plain
PROGRAMW6432 C:\Program Files
PYTHONUNBUFFERED 1
wsgi.errors <_io.TextIOWrapper name='<stderr>' mode='w' encoding='UTF-8'>
RUN_MAIN true
PSMODULEPATH C:\WINDOWS\system32\WindowsPowerShell\v1.0\Modules\;C:\Program Files (x86)\VMware\Infrastructure\vSphere PowerCLI\Modules
wsgi.file_wrapper <class 'wsgiref.util.FileWrapper'>
HOMEPATH \Users\yuan.li
COMSPEC C:\WINDOWS\system32\cmd.exe
USERNAME yuan.li
PATHEXT .COM;.EXE;.BAT;.CMD;.VBS;.VBE;.JS;.JSE;.WSF;.WSH;.MSC;.PY;.PYW
SERVER_NAME vmware-localhost
USERDOMAIN OMNICOM
SCRIPT_NAME 
PYCHARM_HOSTED 1
QUERY_STRING 
PROGRAMDATA C:\ProgramData
NUMBER_OF_PROCESSORS 4
FPS_BROWSER_USER_PROFILE_STRING Default
SERVER_PROTOCOL HTTP/1.1
HTTP_CONNECTION keep-alive
REMOTE_HOST 
HTTP_ACCEPT_LANGUAGE en-GB,en-US;q=0.8,en;q=0.6
HTTP_CACHE_CONTROL max-age=0
HTTP_ACCEPT text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8
wsgi.multiprocess False
ALLUSERSPROFILE C:\ProgramData
SYSTEMROOT C:\WINDOWS
SERVER_SOFTWARE WSGIServer/0.2
PROCESSOR_REVISION 4501
SESSIONNAME Console
HTTP_HOST 127.0.0.1:8000
CONTENT_LENGTH 
PATH C:\Program Files (x86)\Business Objects\Common\3.5\bin\NOTES\;C:\Program Files (x86)\Business Objects\Common\3.5\bin\NOTES\DATA\;C:\Python35-32\Scripts\;C:\Python35-32\;C:\ProgramData\Oracle\Java\javapath;C:\WINDOWS\system32;C:\WINDOWS;C:\WINDOWS\System32\Wbem;C:\WINDOWS\System32\WindowsPowerShell\v1.0\;C:\Program Files (x86)\Sennheiser\SoftphoneSDK\;C:\Python35-32\Scripts;C:\Users\yuan.li\AppData\Local\Microsoft\WindowsApps;C:\Program Files (x86)\Nmap;
OS Windows_NT
PYTHONPATH C:\s14day21
HOMEDRIVE C:
COMMONPROGRAMW6432 C:\Program Files\Common Files
DJANGO_SETTINGS_MODULE s14day21.settings
PROCESSOR_ARCHITECTURE x86
REQUEST_METHOD GET
LOCALAPPDATA C:\Users\yuan.li\AppData\Local
PROCESSOR_LEVEL 6
HTTP_USER_AGENT Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/59.0.3071.115 Safari/537.36
PROGRAMFILES(X86) C:\Program Files (x86)
LOGONSERVER \\MELDC01
wsgi.multithread True
COMMONPROGRAMFILES C:\Program Files (x86)\Common Files
Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/59.0.3071.115 Safari/537.36

注意最后的那个输出结果HTTP_USER_AGENT, 这个是客户端的信息,我们可以根据这个做判断,然后返回不同的结果,比如手机版和PC版返回的显示排版不同。

以上就是Django 获取前端发送的头文件全部内容啦,希望对大家的学习有所帮助,也希望大家继续支持~~

标签: 头文件 Django