I am using Django 1.8.4 and this question is about custom scripts.
I am trying to send an HTML table using a template I made, which needs a array as context. However, I do not know how to pass the array onto the html template.
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)
Of course, this raises TypeError: gethtml() missing 1 required positional argument 'array'.