我正在使用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()缺少1个必需的位置参数'array'。