peterbe / django-static

Template tags for better serving static files from templates in Django
BSD 2-Clause "Simplified" License
193 stars 28 forks source link

Caught UnicodeDecodeError #11

Closed bjornpost closed 13 years ago

bjornpost commented 13 years ago

Hi, just got this error:

Caught UnicodeDecodeError while rendering: ('ascii', ";src:local('\xe2\x98\xba'),", 12, 13, 'ordinal not in range(128)')

Here's the full traceback:

Traceback:
File "../parts/django/django/core/handlers/base.py" in get_response
  100.                     response = callback(request, *callback_args, **callback_kwargs)
File "../parts/feincms/feincms/views/applicationcontent.py" in handler
  35.     response = build_page_response(page, request)
File "../parts/feincms/feincms/views/applicationcontent.py" in build_page_response
  90.     response = _build_page_response(page, request)
File "../parts/feincms/feincms/views/base.py" in _build_page_response
  15.         }, context_instance=RequestContext(request, extra_context))
File "../parts/django/django/shortcuts/__init__.py" in render_to_response
  20.     return HttpResponse(loader.render_to_string(*args, **kwargs), **httpresponse_kwargs)
File "../parts/django/django/template/loader.py" in render_to_string
  186.     return t.render(context_instance)
File "../parts/django/django/template/__init__.py" in render
  173.             return self._render(context)
File "../parts/django/django/test/utils.py" in instrumented_test_render
  54.     return self.nodelist.render(context)
File "../parts/django/django/template/__init__.py" in render
  796.                 bits.append(self.render_node(node, context))
File "../parts/django/django/template/debug.py" in render_node
  72.             result = node.render(context)
File "../parts/django/django/template/loader_tags.py" in render
  125.         return compiled_parent._render(context)
File "../parts/django/django/test/utils.py" in instrumented_test_render
  54.     return self.nodelist.render(context)
File "../parts/django/django/template/__init__.py" in render
  796.                 bits.append(self.render_node(node, context))
File "../parts/django/django/template/debug.py" in render_node
  72.             result = node.render(context)
File "build/bdist.macosx-10.6-universal/egg/django_static/templatetags/django_static.py" in render
  189.                             symlink_if_possible=self.symlink_if_possible)
File "build/bdist.macosx-10.6-universal/egg/django_static/templatetags/django_static.py" in _static_file
  507.             content = REFERRED_CSS_URLS_REGEX.sub(replacer, content)
File "build/bdist.macosx-10.6-universal/egg/django_static/templatetags/django_static.py" in replacer
  498.                                             warn_no_file=DEBUG and True or False)
File "build/bdist.macosx-10.6-universal/egg/django_static/templatetags/django_static.py" in _static_file
  507.             content = REFERRED_CSS_URLS_REGEX.sub(replacer, content)

Exception Type: TemplateSyntaxError at /
Exception Value: Caught UnicodeDecodeError while rendering: ('ascii', ";src:local('\xe2\x98\xba'),", 12, 13, 'ordinal not in range(128)')

I guess it has something to do with the following lines in my CSS (especially the 'smiley' char):

@font-face {
font-family: 'da39a3ee5e';
src: url('da39a3ee5e.eot');
src: local('☺'), url(data:font/woff;charset=utf-8;base64,[ snip ]) format('truetype');
font-weight: normal;
font-style: normal;
}

Which is a common technique for embedding webfonts.

peterbe commented 13 years ago

Cheers. As that CSS doesn't come with a encoding declared I guess you'll have to assume it's UTF-8.

peterbe commented 13 years ago

Bug fixed. Please see https://github.com/peterbe/django-static/commit/0cc1ce2ffc1661377d5cd957d6c4048989d5b5f7 Unfortunately this commit diff also shows some of the line ending space trimmings I've configured my editor to do these days.

bjornpost commented 13 years ago

Nice, thank you. I've found another bug, regarding your CSS url() regexp. You're matching against every url(), but that gives me some weird results (I'm using inline data). So instead I guess you want to match against something like this:

url\((?!data:)([^\)])+\)

Which matches url('somefile.jpg') but ignores inline data, which looks like url(data:...). I didn't test the regexp, but I think it should work ok.

peterbe commented 13 years ago

Would you mind making a fork that I can pull and run tests against?

bjornpost commented 13 years ago

Sure thing, I'll send a pull request when I have it working :).