Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save quevon24/642d13db020b1ca35db4653a3948d6bc to your computer and use it in GitHub Desktop.
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.
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