0

I created a site where my techs submit their inventory using model forms. Everything is working as intended but I would like to add the function of sending the whole form as an email when they submit their inventory. This would allow for my inventory team to verify counts without having to log in and check the website.

Here is my view.py I know it works if I remove the email bits and saves to my models. Currently returns an error: 'dict' object has no attribute 'splitlines'

    form = Inventory_Form()
    if request.method == 'POST':
        form = Inventory_Form(request.POST)
        tech_field = form.save(commit=False)
        tech_field.technician = request.user
        tech_field.save()
        if form.is_valid():
            form.save()
            name = form.cleaned_data['initials_date']
            from_email = 'operations@imbadatthis.com'
            subject = 'Weekly Inventory', form.cleaned_data['initials_date']
            message = form.cleaned_data
            try:
                send_mail(subject, message, from_email, ['myemail@n00b.com'], name)
            except BadHeaderError:
                return HttpResponse('Invalid header found.')

            return response, redirect('inventory_submitted')

    return render(request, 'inventory.html', {'form': form})

Would it be better to save the form to a csv then attach it as an email? I looked at this and also had issues with that part.

1 Answers1

1

I guess the error is raised at the send_mail because of

 message = form.cleaned_data

Because this is a dict and the send_mail from django expects the message to be a string.

You have to convert the dict to a string.

Maybe this helps to make a nice looking email. (documentation)

bb4L
  • 899
  • 5
  • 16