Forked from renyi/django-wkhtmltopdf-static-url-hack.py
Created
December 22, 2017 04:10
-
-
Save quevon24/642d13db020b1ca35db4653a3948d6bc 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