django views.py视图 获取用户请求相关信息以及请求头

Posted minger_lcm

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了django views.py视图 获取用户请求相关信息以及请求头相关的知识,希望对你有一定的参考价值。

 

 

请求的其他信息

用户发来请求时候,不仅发来数据,也把请求头也发过来

 

 

 

在views.py 怎么找请求数据?

request是一个对象,这个对象封装很多信息,可以先查这个对象的类

 

print(type(request))

from django.shortcuts import reverse
def index(request):
    v = reverse(\'author:index\')
    print(v)
    print(type(request))

    # 找到这个类 导入这个类 <class \'django.core.handlers.wsgi.WSGIRequest\'>
    from django.core.handlers.wsgi import WSGIRequest
    

    return HttpResponse("index")

 

去查看这个类代码

 

 

请求相关信息都放在这里,request.POST,request.GET 都是在这里提取出来

 

 

封装了所有用户请求信息

request.environ

from django.shortcuts import reverse
def index(request):
    v = reverse(\'author:index\')
    print(v)
    print(type(request))

    # 找到这个类 导入这个类 <class \'django.core.handlers.wsgi.WSGIRequest\'>
    from django.core.handlers.wsgi import WSGIRequest


    # 封装了所有用户请求信息
    print(request.environ)

    \'\'\'
    {\'ALLUSERSPROFILE\': \'C:\\\\ProgramData\', \'APPDATA\': \'C:\\\\Users\\\\Administrator\\\\AppData\\\\Roaming\', \'COMMONPROGRAMFILES\': \'C:\\\\Program Files\\\\Common Files\', \'COMMONPROGRAMFILES(X86)\': \'C:\\\\Program Files (x86)\\\\Common Files\', \'COMMONPROGRAMW6432\': \'C:\\\\Program Files\\\\Common Files\', \'COMPUTERNAME\': \'QH-20181120YSCF\', \'COMSPEC\': \'C:\\\\Windows\\\\system32\\\\cmd.exe\', \'DJANGO_SETTINGS_MODULE\': \'day21django.settings\', \'FP_NO_HOST_CHECK\': \'NO\', \'HOMEDRIVE\': \'C:\', \'HOMEPATH\': \'\\\\Users\\\\Administrator\', \'LOCALAPPDATA\': \'C:\\\\Users\\\\Administrator\\\\AppData\\\\Local\', \'LOGONSERVER\': \'\\\\\\\\QH-20181120YSCF\', \'NUMBER_OF_PROCESSORS\': \'4\', \'OS\': \'Windows_NT\', \'PATH\': \'C:\\\\Windows\\\\system32;C:\\\\Windows;C:\\\\Windows\\\\System32\\\\Wbem;C:\\\\Windows\\\\System32\\\\WindowsPowerShell\\\\v1.0\\\\;C:\\\\Program Files (x86)\\\\NVIDIA Corporation\\\\PhysX\\\\Common;C:\\\\Program Files\\\\Intel\\\\WiFi\\\\bin\\\\;C:\\\\Program Files\\\\Common Files\\\\Intel\\\\WirelessCommon\\\\;C:\\\\Users\\\\Administrator\\\\AppData\\\\Local\\\\Programs\\\\Python\\\\Python36;C:\\\\Users\\\\Administrator\\\\AppData\\\\Local\\\\Programs\\\\Python\\\\Python36\\\\Scripts\', \'PATHEXT\': \'.COM;.EXE;.BAT;.CMD;.VBS;.VBE;.JS;.JSE;.WSF;.WSH;.MSC\', \'PROCESSOR_ARCHITECTURE\': \'AMD64\', \'PROCESSOR_IDENTIFIER\': \'Intel64 Family 6 Model 69 Stepping 1, GenuineIntel\', \'PROCESSOR_LEVEL\': \'6\', \'PROCESSOR_REVISION\': \'4501\', \'PROGRAMDATA\': \'C:\\\\ProgramData\', \'PROGRAMFILES\': \'C:\\\\Program Files\', \'PROGRAMFILES(X86)\': \'C:\\\\Program Files (x86)\', \'PROGRAMW6432\': \'C:\\\\Program Files\', \'PSMODULEPATH\': \'C:\\\\Windows\\\\system32\\\\WindowsPowerShell\\\\v1.0\\\\Modules\\\\\', \'PUBLIC\': \'C:\\\\Users\\\\Public\', \'PYCHARM_HOSTED\': \'1\', \'PYTHONIOENCODING\': \'UTF-8\', \'PYTHONPATH\': \'C:\\\\Users\\\\Administrator\\\\PycharmProjects\\\\SSSSS\\\\SS\\\\day21\\\\day21django\', \'PYTHONUNBUFFERED\': \'1\', \'SESSIONNAME\': \'Console\', \'SYSTEMDRIVE\': \'C:\', \'SYSTEMROOT\': \'C:\\\\Windows\', \'TEMP\': \'C:\\\\Users\\\\ADMINI~1\\\\AppData\\\\Local\\\\Temp\', \'TMP\': \'C:\\\\Users\\\\ADMINI~1\\\\AppData\\\\Local\\\\Temp\', \'USERDOMAIN\': \'QH-20181120YSCF\', \'USERNAME\': \'Administrator\', \'USERPROFILE\': \'C:\\\\Users\\\\Administrator\', \'WINDIR\': \'C:\\\\Windows\', \'WINDOWS_TRACING_FLAGS\': \'3\', \'WINDOWS_TRACING_LOGFILE\': \'C:\\\\BVTBin\\\\Tests\\\\installpackage\\\\csilogfile.log\', \'_DFX_INSTALL_UNSIGNED_DRIVER\': \'1\', \'RUN_MAIN\': \'true\', \'SERVER_NAME\': \'QH-20181120YSCF\', \'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\': \'/a/index/\', \'QUERY_STRING\': \'\', \'REMOTE_ADDR\': \'127.0.0.1\', \'CONTENT_TYPE\': \'text/plain\', \'HTTP_HOST\': \'127.0.0.1:8000\', \'HTTP_CONNECTION\': \'keep-alive\', \'HTTP_CACHE_CONTROL\': \'max-age=0\', \'HTTP_UPGRADE_INSECURE_REQUESTS\': \'1\', \'HTTP_USER_AGENT\': \'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.110 Safari/537.36\', \'HTTP_ACCEPT\': \'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8\', \'HTTP_ACCEPT_ENCODING\': \'gzip, deflate, br\', \'HTTP_ACCEPT_LANGUAGE\': \'zh-CN,zh;q=0.9\', \'HTTP_COOKIE\': \'csrftoken=E0U5wT67AQMApvIkPM5Cl37WNV3QOwT6ejf7AO4wQXsVMkJvGzDNAEBEZxFJbwjg\', \'wsgi.input\': <_io.BufferedReader name=740>, \'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\'>, \'CSRF_COOKIE\': \'E0U5wT67AQMApvIkPM5Cl37WNV3QOwT6ejf7AO4wQXsVMkJvGzDNAEBEZxFJbwjg\'}

    \'\'\'

    return HttpResponse("index")

 

常用的请求参数
request.GET
request.POST
request.COOKIES


拿到请求头,控制用户用什么终端访问的
    print(request.environ[\'HTTP_USER_AGENT\'])
    # Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.110 Safari/537.36

 

显示访问网站以什么终端发来请求 (安卓,ios,pc端)
还有作用不同终端访问这个网站时候,显示不同页面,比如手机的访问手机端页面,电脑端访问电脑端页面
 
通过请求头,分辨出不同终端 访问不同页面
 

 

以上是关于django views.py视图 获取用户请求相关信息以及请求头的主要内容,如果未能解决你的问题,请参考以下文章

Django views.py 视图函数

每次请求视图时,django 是不是都会编译 views.py?

Django视图层(views.py)

在views.py中获取url - python django

Django之views.py视图函数学习

django之视图系统 views.py