Attributeerror: 'Asgirequest' Object has no attribute 'user_agent'

Asked

Viewed 93 times

0

Replaces my WSGI for ASGI to rotate the Django Channels, as is said in documentation of them. The problem is that the user_agent, where I detect whether it’s mobile or not. I had created a function on context_processors that worked, but stopped when I switched to ASGI.

context_processors.py

def is_mobile(request):
    is_mobile = False;

    if 'HTTP_USER_AGENT' in request.META:
        user_agent = request.META['HTTP_USER_AGENT']

        # Test common mobile values.
        pattern = "(up.browser|up.link|mmp|symbian|smartphone|midp|wap|phone|windows ce|pda|mobile|mini|palm|netfront)"
        prog = re.compile(pattern, re.IGNORECASE)
        match = prog.search(user_agent)

        if match:
            is_mobile = True;
        else:
            # Nokia like test for WAP browsers.
            # http://www.developershome.com/wap/xhtmlmp/xhtml_mp_tutorial.asp?page=mimeTypesFileExtension

            if 'HTTP_ACCEPT' in request.META:
                http_accept = request.META['HTTP_ACCEPT']

                pattern = "application/vnd\.wap\.xhtml\+xml"
                prog = re.compile(pattern, re.IGNORECASE)

                match = prog.search(http_accept)

                if match:
                    is_mobile = True

        if not is_mobile:
            # Now we test the user_agent from a big list.
            user_agents_test = ("w3c ", "acs-", "alav", "alca", "amoi", "audi",
                                "avan", "benq", "bird", "blac", "blaz", "brew",
                                "cell", "cldc", "cmd-", "dang", "doco", "eric",
                                "hipt", "inno", "ipaq", "java", "jigs", "kddi",
                                "keji", "leno", "lg-c", "lg-d", "lg-g", "lge-",
                                "maui", "maxo", "midp", "mits", "mmef", "mobi",
                                "mot-", "moto", "mwbp", "nec-", "newt", "noki",
                                "xda",  "palm", "pana", "pant", "phil", "play",
                                "port", "prox", "qwap", "sage", "sams", "sany",
                                "sch-", "sec-", "send", "seri", "sgh-", "shar",
                                "sie-", "siem", "smal", "smar", "sony", "sph-",
                                "symb", "t-mo", "teli", "tim-", "tosh", "tsm-",
                                "upg1", "upsi", "vk-v", "voda", "wap-", "wapa",
                                "wapi", "wapp", "wapr", "webc", "winw", "winw",
                                "xda-",)

            test = user_agent[0:4].lower()
            if test in user_agents_test:
                is_mobile = True

    request.is_mobile = is_mobile

    return {}

When I try to access this method in my view, this error occurs:

if request.is_mobile:
AttributeError: 'AsgiRequest' object has no attribute 'is_mobile'
  • Tried to request.user_agent.is_mobile?

  • tried, he also says he does not have the attributo user_agent. It started right after I switched from WSGI to ASGI.

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.