简体   繁体   English

Django-wkhtmltopdf - 动态模板

[英]Django-wkhtmltopdf - dynamic templates

I'm wondering if it is possible to dynamically assign the template_name based on data provided to my class-based view using a form?我想知道是否可以使用表单根据提供给我的基于类的视图的数据动态分配template_name

for example I have the following form page:例如我有以下表单页面:

views.py视图.py

def form_generate_pdf(request, project_id):
   """Form to collect data to insert into PDF"""

   project = Project.objects.get(id=project_id)

   if request.method == 'POST':
      form = PDFForm(request.POST)
      if form.is_valid():
         request.session['field1'] = form.cleaned_data.get('field1')
         return redirect('myapp:generate_pdf')

   else:
      form = PDFForm()

   context = {
      'project': project,
      'form': form
   }

And the page that generates the PDF:以及生成PDF的页面:

views.py视图.py

class Generate_PDF(PDFTemplateView):
   """Generate a PDF using the values from form_generate_pdf"""

   filename = "test.pdf"
   # I want the field below to be dynamic depending on the value of field1 from the view form_generate_pdf
   template_name = "myapp/pdf.html"
   cmd_options = {
      'page-size': 'Letter',
      'viewport-size' : '1920x1080',
      'footer-left' : '[page]/[topage]',
      'no-outline' : True,
   }

   def get_context_data(self, **kwargs):
      context = super(Generate_PDF, self).get_context_data(**kwargs)
      field1 = self.request.session['field1']
      context['field1'] = field1
      }

I would say I'm intermediate with django but I'm still fairly new to python and django in general (~5 months of playing now), I don't seem to have enough understanding to figure out how to insert some logic into the Generate_PDF class to programatically select the template type.我想说我是 django 的中级,但我对 python 和 django 还是很陌生(现在玩了大约 5 个月),我似乎对插入逻辑没有足够的理解来弄清楚如何Generate_PDF class 以编程方式 select 模板类型。 Something like:就像是:

if field1 == 'this':
   template_name = 'myapp/pdf_1.html'
else:
   template_name = 'myapp/pdf_2.html'

The problem is my view can't seem to see the field1 variable.问题是我的视图似乎看不到field1变量。

Perhaps I am going about this the wrong way as well.也许我也在以错误的方式解决这个问题。 I could have my form redirect to unique class based views I suppose, but even though that would solve my issue it feels very repetitive and incorrect to me.我想我可以让我的表单重定向到基于独特的 class 的视图,但即使这可以解决我的问题,它对我来说感觉非常重复和不正确。

After looking at more examples, documentation (sparse for the django-wkhtmltopdf module, unfortunately), and the github repository for the module;在查看了更多示例、文档(不幸的是,django-wkhtmltopdf 模块稀疏)和模块的 github 存储库之后; I figured out how to override the the get_filename and get_template_name function.我想出了如何覆盖get_filenameget_template_name function。

class Generate_PDF(PDFTemplateView):
   """Generate a PDF using the values from form_generate_pdf"""

   # filename = "test.pdf"
   # I want the field below to be dynamic depending on the value of field1 from the view form_generate_pdf
   # template_name = "myapp/pdf.html"
   cmd_options = {
      'page-size': 'Letter',
      'viewport-size' : '1920x1080',
      'footer-left' : '[page]/[topage]',
      'no-outline' : True,
   }

   # I found this looking at the github repository
   def get_filename(self):
     if self.field1 == 'foo':
       filename = 'bar1.pdf'
     else:
       filename = 'bar2.pdf'
     return filename

   # I found this looking at the Django documentation
   def get_template_names(self):
     if self.field1 == 'foo':
       template_name= 'myapp/bar1.html'
     else:
       template_name= 'myapp/bar2.html'
     return template_name

   def get_context_data(self, **kwargs):
      context = super(Generate_PDF, self).get_context_data(**kwargs)
      self.field1 = self.request.session['field1']
      context['field1'] = field1
      }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM