Created
June 30, 2016 02:56
-
-
Save sjkingo/7dc47ba0adf40e23d5cc37784b342a56 to your computer and use it in GitHub Desktop.
Redirecting to a PDF when "View on site" is clicked in Django admin
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
from django.db import models | |
class SomeModel(models.Model): | |
# fields here | |
class Meta: | |
pass | |
def get_absolute_url(self): | |
return reverse('pdf_view', id=self.id) |
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
from django.http import HttpResponse | |
from django.template.loader import get_template | |
import pywkhtmltopdf as pdf | |
def pdf_view(request, template='templates/pdf.html'): | |
c = pdf.HTMLToPDFConverter() | |
context = {} # something | |
html = get_template(template).render(context) | |
data = c.convert(html) | |
response = HttpResponse(data, content_type='application/pdf') | |
response['Content-Disposition'] = 'attachment; filename=something.pdf' | |
return response |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment