selwin / django-user_agents

A django package that allows easy identification of visitor's browser, OS and device information, including whether the visitor uses a mobile phone, tablet or a touch capable device.
MIT License
640 stars 104 forks source link

Fixed: crash when request has no META attribute #8

Closed rafaelks closed 8 years ago

rafaelks commented 9 years ago

I'm having crash using template tag user_agents, because when I'm compressing my JavaScript & CSS files on deploy (using django-compressor) the request doesn't have the META attribute.

Log

Traceback (most recent call last):
  File "manage.py", line 10, in <module>
    execute_from_command_line(sys.argv)
  File "/home/cm/envs/cm/local/lib/python2.7/site-packages/django/core/management/__init__.py", line 385, in execute_from_command_line
    utility.execute()
  File "/home/cm/envs/cm/local/lib/python2.7/site-packages/django/core/management/__init__.py", line 377, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/home/cm/envs/cm/local/lib/python2.7/site-packages/django/core/management/base.py", line 288, in run_from_argv
    self.execute(*args, **options.__dict__)
  File "/home/cm/envs/cm/local/lib/python2.7/site-packages/django/core/management/base.py", line 338, in execute
    output = self.handle(*args, **options)
  File "/home/cm/envs/cm/local/lib/python2.7/site-packages/django/core/management/base.py", line 533, in handle
    return self.handle_noargs(**options)
  File "/home/cm/envs/cm/local/lib/python2.7/site-packages/compressor/management/commands/compress.py", line 274, in handle_noargs
    self.compress(sys.stdout, **options)
  File "/home/cm/envs/cm/local/lib/python2.7/site-packages/compressor/management/commands/compress.py", line 221, in compress
    rendered = parser.render_nodelist(template, context, node)
  File "/home/cm/envs/cm/local/lib/python2.7/site-packages/compressor/offline/django.py", line 114, in render_nodelist
    return node.nodelist.render(context)
  File "/home/cm/envs/cm/local/lib/python2.7/site-packages/django/template/base.py", line 844, in render
    bit = self.render_node(node, context)
  File "/home/cm/envs/cm/local/lib/python2.7/site-packages/django/template/base.py", line 858, in render_node
    return node.render(context)
  File "/home/cm/envs/cm/local/lib/python2.7/site-packages/django/template/defaulttags.py", line 305, in render
    match = condition.eval(context)
  File "/home/cm/envs/cm/local/lib/python2.7/site-packages/django/template/defaulttags.py", line 898, in eval
    return self.value.resolve(context, ignore_failures=True)
  File "/home/cm/envs/cm/local/lib/python2.7/site-packages/django/template/base.py", line 624, in resolve
    new_obj = func(obj, *arg_vals)
  File "/home/cm/envs/cm/local/lib/python2.7/site-packages/django_user_agents/templatetags/user_agents.py", line 21, in is_tablet
    return get_and_set_user_agent(request).is_tablet
  File "/home/cm/envs/cm/local/lib/python2.7/site-packages/django_user_agents/utils.py", line 41, in get_and_set_user_agent
    request.user_agent = get_user_agent(request)
  File "/home/cm/envs/cm/local/lib/python2.7/site-packages/django_user_agents/utils.py", line 26, in get_user_agent
    ua_string = request.META.get('HTTP_USER_AGENT', '')
AttributeError: 'NoneType' object has no attribute 'META'
nikolas commented 8 years ago

This looks good. @selwin what do you think?