-1

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'.

M.javid
  • 6,387
  • 3
  • 41
  • 56
Brian Pace
  • 39
  • 1
  • 9
  • You are not passing the `request` to `get_html`, which is why it raises `TypeError`. Tbh the question is very vague – zanderle Sep 09 '15 at 11:23
  • Basically, I want to render an html for the body of an e-mail. However, I do not know how to pass the request to gethtml() along with context, which is an array of objects – Brian Pace Sep 10 '15 at 08:18
  • Which request are you trying to pass? – zanderle Sep 10 '15 at 08:24
  • I actually do not know how to just pass in the array. I want to access an html file and fill it with the array. I tried just passing the array but the render was not happy, since it needs it. – Brian Pace Sep 10 '15 at 08:36
  • Just remove `request` from `gethtml` definition and define `as_file` in a different way. If you only want to pass array to `gethtml` it shouldn't take in the `request` as well... – zanderle Sep 10 '15 at 08:47

1 Answers1

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

works like a charm

Brian Pace
  • 39
  • 1
  • 9