如果jinja标记是字符串格式,我如何在django和jinja2中使用它?

时间:2021-11-20 00:01:29

Part 1 of this question asked and answered separately.

该问题的第1部分单独提出并回答。

I have a Report and a ReportTemplate.

我有一份报告和一份ReportTemplate。

+----+----------+---------------+-------------+
| id |  title   |     data      | template_id |
+----+----------+---------------+-------------+
|  1 | report 1 | {data: [...]} |           1 |
+----+----------+---------------+-------------+

reports table

+----+-----------+---------------+------------+
| id |   title   |    markup     |    css     |
+----+-----------+---------------+------------+
|  1 | template1 | <doctype!>... | body {.... |
+----+-----------+---------------+------------+

templates table

A Report belongs to a ReportTemplate. A ReportTemplate has many Report.

报告属于ReportTemplate。 ReportTemplate有很多报告。

I have a custom admin action for Report in admin.py called print_as_pdf

我在admin.py中有一个名为print_as_pdf的自定义管理操作

import logging

logger = logging.getLogger('reports.admin')

from django.contrib import admin

# Register your models here.
from reports.models import Report, ReportTemplate

class ReportAdmin(admin.ModelAdmin):
    fields = ['commodity', 
    'date',
    'trade_period',
    'quantity_cutoff',
    'data',
    'template',
    'title']

    actions = ['print_as_pdf']

def print_as_pdf(self, request, queryset):
    logger.debug('anything')
    for report in queryset:
        markup = report.template.markup
        logger.debug(markup)
    return
print_as_pdf.short_description = 'Generate as pdf'

These are models:

这些是模型:

class ReportTemplate(models.Model):
    title = models.CharField(max_length=50)
    markup = models.TextField(default = 'markup here...')
    styles = models.TextField(default = 'styles here...')

    # __unicode__ on Python 2
    # __str__ on Python 3
    def __unicode__(self):
        return self.title

class Report(models.Model):
    title = models.CharField(max_length=50)
    commodity = models.CharField(max_length=10)
    date = models.DateTimeField('date traded')
    trade_period = models.CharField(max_length=10, default='open')
    quantity_cutoff = models.IntegerField(default=0)
    printed = models.BooleanField(default=0)
    datetime_email_sent = models.DateTimeField('date email sent', blank=True, null=True)
    data = models.TextField(default = 'data here...')
    template = models.ForeignKey(ReportTemplate)

What I want to do is:

我想做的是:

  1. retrieve the associated ReportTemplate and its markup field value
  2. 检索关联的ReportTemplate及其标记字段值
  3. put the data field value of the Report through the markup value in 1 which is written with jinja2 markup
  4. 将报告的数据字段值通过标记值1放入jinja2标记
  5. use weasyprint and print out the data-filled markup from 2 as pdf
  6. 使用weasyprint并从2打印出数据填充标记为pdf

I am stuck at step 2.

我陷入了第2步。

Since the markup I have retrieved is in a string format, how do I run it through with the data I have?

由于我检索的标记是字符串格式,如何使用我拥有的数据运行它?

1 个解决方案

#1


1  

Adjusting from Jinja 2 documentation, it could be as simple as

从Jinja 2文档调整,它可以很简单

>>> template = Template(report.markup)
>>> template.render(report=report)
<html>...

If you want to store the output into another variable

如果要将输出存储到另一个变量中

>>> final_markup = template.render(report=report)

provided that your templates expect to get the whole report as the report template parameter.

只要您的模板希望将整个报告作为报告模板参数。

#1


1  

Adjusting from Jinja 2 documentation, it could be as simple as

从Jinja 2文档调整,它可以很简单

>>> template = Template(report.markup)
>>> template.render(report=report)
<html>...

If you want to store the output into another variable

如果要将输出存储到另一个变量中

>>> final_markup = template.render(report=report)

provided that your templates expect to get the whole report as the report template parameter.

只要您的模板希望将整个报告作为报告模板参数。