My template doesn't found - jinja2.exceptions.TemplateNotFound

46 views Asked by At

I'm developing a backend in Django rest framework and one of my endpoints must to generate a report in pdf, I've been watching several examples and found the library pdfkit which is used to do it in conjunction with jinja2, I created a template in html and I created a function that generate the pdf, but in the moment that is called, it throws me this error:

    raise TemplateNotFound(template)
jinja2.exceptions.TemplateNotFound: template_convocatoria.html

I've reviewed the path many times, but the error stills.

I'll share the code of the function that generates the PDF:

def generar_pdf_convocatoria(info, rutacss=''):
  ruta_template = os.path.join(settings.BASE_DIR, 'administration/extras/template_convocatoria.html')
  print(ruta_template)
  nombre_template = ruta_template.split('/')[-1]
  print(nombre_template)
  env = jinja2.Environment(loader=jinja2.FileSystemLoader(ruta_template))
  template = env.get_template(nombre_template)
  html = template.render(info)

  options = {
      'page-size': 'Letter',
      'margin-top': '0.05in',
      'margin-right': '0.05in',
      'margin-bottom': '0.05in',
      'margin-left': '0.05in',
      'encoding': 'UTF-8'
  }

  config = pdfkit.configuration(wkhtmltopdf='/usr/bin/wkhtmltopdf')
  ruta_salida = os.path.join(settings.BASE_DIR, 'administration/extras/archivo.pdf')

  pdfkit.from_string(html, ruta_salida, css=rutacss, options=options, configuration=config)
0

There are 0 answers