Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Recommend defaulting content_type to PDF #75

Open
jacklinke opened this issue Nov 18, 2023 · 1 comment
Open

Recommend defaulting content_type to PDF #75

jacklinke opened this issue Nov 18, 2023 · 1 comment

Comments

@jacklinke
Copy link

This issue is somewhat selfish in nature. I prefer to use Function-Based Views, and would like to use WeasyTemplateResponse in these views with minimal boilerplate. Specifying content_type in the returned response should no longer be necessary.

As noted in the README, support for PNG output in Weasyprint has been removed, so we can default to using 'application/pdf' as the content_type for WeasyTemplateResponse.

Additionally, this can be done in an entirely backwards-compatible manner by setting the content_type in the init method of WeasyTemplateResponse, defaulting to a pdf if not otherwise set: self.content_type = kwargs.get("content_type", "application/pdf")

Then all we need for a view is:

from django_weasyprint.views import WeasyTemplateResponse

def pdf_view(request, id):
    template = "detail_view.html"
    pdf_filename = "my_report.pdf"

    context = {}
    context["data"] = MyModel.objects.get(id=id)

    return WeasyTemplateResponse(pdf_filename, request=request, template=template, context=context)

Would a PR with this small change and an example for the docs be helpful?

@fdemmer
Copy link
Owner

fdemmer commented Dec 6, 2023

Sounds reasonable... a PR is very welcome! Please include a test.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants