【问题标题】:Sending a parameter to the render function of pdf向pdf的渲染函数发送参数
【发布时间】:2022-01-01 14:28:47
【问题描述】:

我正在尝试在 Django 中将 html 呈现为 pdf 的月度报告,该报告按月从数据库中获取数据。 我需要将 html 页面上选定的月份发送到创建 Pdf 的类。我该怎么做?

我的视图类:

class view_pdf_monthly_report(View):
    def get(self, request, *args, **kwargs):
        push = {'month':month}
        pdf = render_to_pdf('home/pdf_monthly_inquiries_report.html', push)
        return HttpResponse(pdf, content_type='application/pdf')

我的 html:如何从这里发送月份?

<div class="text-center">
<a class="btn btn-info" href="{% url 'view_pdf_monthly_report'%}"  target="_blank">View PDF</a>
</div>

谢谢!

【问题讨论】:

  • 您可以将表单与 get 方法一起使用,当您提交表单时,您可以从请求中获取您的月份。
  • 是的,这是我在 html 文件中所做的,但我的问题是我如何将这个月发送到将其转换为 pdf 的类?谢谢
  • 你好@Michaelilkanayev 检查我的答案

标签: html css django class pdf


【解决方案1】:

首先创建一个简单的表单,它将月份数据发送到您的视图

<div class="text-center">
 <form action="{% url 'view_pdf_monthly_report'%}" method="get">
  <input type="hidden" value="{{month}}" name="month">
  <button class="btn btn-info" type="sumbit">View PDF</button>
 </form>
</div>

然后在你的视野中

class view_pdf_monthly_report(View):
    def get(self, request, *args, **kwargs):
        month = request.GET.get('month')
        push = {'month':month}
        pdf = render_to_pdf('home/pdf_monthly_inquiries_report.html', push)
        return HttpResponse(pdf, content_type='application/pdf')

【讨论】:

    猜你喜欢
    • 2013-01-30
    • 2015-02-17
    • 1970-01-01
    • 2018-06-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-03-16
    • 2019-01-19
    相关资源
    最近更新 更多