zoukankan      html  css  js  c++  java
  • Python学习---Django的request扩展[获取用户设备信息]

    关于Django的request扩展【获取用户设备信息】

    settings.py

    INSTALLED_APPS = [
       ...
     'app01',   # 注册app
    ]
    STATICFILES_DIRS = (os.path.join(BASE_DIR, "statics"),)  # 现添加的配置,这里是元组,注意逗号
    TEMPLATES = [
       ...
       'DIRS': [os.path.join(BASE_DIR, 'templates')],
    ]

    urls.py

    from django.contrib import admin
    from django.urls import path
    from django.conf.urls import url, include
    from app01 import views
    urlpatterns = [
       # 获取设备信息【纯属测试】
     url(r'^testDevice.html', views.testDevice),
    ]

    views.py

    from django.shortcuts import render, redirect
    from app01 import models
    # 测试设备信息
    def testDevice(request):
        # 这里的request是一个对象
        print(type(request))   # <class 'django.core.handlers.wsgi.WSGIRequest'>
        from django.core.handlers.wsgi import WSGIRequest
        print('请求相关的信息:', request.environ)  # environ里面有请求的所有信息
        print('设备信息:', request.environ.get("HTTP_USER_AGENT"))  # 全部返回的是个字典
        return HttpResponse("OK")

    templates/index.html、

    image

    页面显示;

    image

    应用场景:根据移动和PC端访问的方式不同,页面呈现的效果不同

    image

  • 相关阅读:
    自我介绍 Self Introduction
    HDU1864 最大报销额
    HDU2955 Robberies
    Sicily 1509. Rails
    Sicily 1031. Campus
    Sicily 1090. Highways
    Sicily 1034. Forest
    Sicily 1800. Sequence
    Sicily 1150. 简单魔板
    CodeVS4919 线段树练习4
  • 原文地址:https://www.cnblogs.com/ftl1012/p/9410803.html
Copyright © 2011-2022 走看看