Skip to content

Instantly share code, notes, and snippets.

@renyi
Created May 21, 2016 10:38
Show Gist options
  • Save renyi/f02b4322590e9288ac679545df4748d3 to your computer and use it in GitHub Desktop.
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.
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