-1

我正在使用 Django 1.8.4,这个问题是关于自定义脚本的。

我正在尝试使用我制作的模板发送 HTML 表,该模板需要一个数组作为上下文。但是,我不知道如何将数组传递给 html 模板。

from django.core.management.base import BaseCommand
from django.core.mail import send_mail
from django.conf import settings
from django.shortcuts import render
from django.template.loader import render_to_string
from manager.models import Equipment, Employee


def gethtml(request, array):
    as_file = request.GET.get('as_file')
    context = {'array': array}

    if as_file:
        content = render_to_string('email.html', context)
        with open('email.html', 'w') as static_file:
            static_file.write(content)

    return render('email.html', context)


def getequips(notcals, employeeobj):
    equiplist = []
    for eq in notcals:
        if employeeobj.clientID == eq.asset.organisation:
            equiplist.append(eq)
    return equiplist  # returns non-calibrated equipment


def getemps():
    notCalibrated = []
    for eq in Equipment.objects.all():
        if not eq.isCalibrated():
            notCalibrated.append(eq)
    emps = []
    for a in Employee.objects.all():
        print(Employee.objects.all())
        equips = getequips(notCalibrated, a)  # has all the non-calibrated equipment of a given employee
        emps.append(equips)
    return emps  # returns the employee list containing equipment


class Command(BaseCommand):
    help = 'Sends emails for uncalibrated equipment'

    def handle(self, *args, **options):
        clientlist = []
        subject = "Inspection Notice"
        from_email = settings.EMAIL_HOST_USER
        emps = getemps()
        for a in emps:  # the employees with all the uncalibrated equipment
            if a:
                html = gethtml(a)

当然,这会引发 TypeError: gethtml() missing 1 required positional argument 'array'。

4

1 回答 1

0
for a in emps:  # the employees with all the uncalibrated equipment
            if a:
                rendered = render_to_string('email.html', {'array': a})

奇迹般有效

于 2015-09-10T11:45:13.507 回答