Created
May 21, 2016 10:38
-
-
Save renyi/f02b4322590e9288ac679545df4748d3 to your computer and use it in GitHub Desktop.
django-wkhtmltopdf STATIC_URL hack to make static files work on both local and remote hosting.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def render_to_response(self, context, **response_kwargs): | |
from django.conf import settings | |
STATIC_URL = settings.STATIC_URL | |
if 'http' not in STATIC_URL: | |
# wkhtmltopdf requires full uri to load css | |
from urlparse import urlparse | |
parsed = urlparse(self.request.META.get('HTTP_REFERER')) | |
parsed = '{uri.scheme}://{uri.netloc}'.format(uri=parsed) | |
context["STATIC_URL"] = "{}{}".format(parsed, settings.STATIC_URL) | |
else: | |
context["STATIC_URL"] = settings.STATIC_URL | |
return PDFTemplateResponse( | |
request=self.request, | |
template=self.template_name, | |
context=context, | |
filename='valuation-#{}.pdf'.format(self.object.code), | |
) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment