views:

39

answers:

2

Hello, another Django send_mail question. Seems like I have problems displaying data in an email that separate form from function. Seems like this is a variable problem.

Edit: I manage to make the client name show up! Now how can to the same thing with invoice. Say that I wanted to display the date, invoice_no, work_orders & contract_info?

    #models.py
    class Invoice(models.Model):
    client = models.ForeignKey(Client)
    date = models.DateField()
    invoice_no = models.CharField(max_length=16)
    work_orders = models.ManyToManyField(Work_Order)
    contract_info = models.ForeignKey(Contract_Info)

    def __unicode__(self):
                return self.invoice_no

    #views.py
    @login_required
    def invoice_mail(request, id=1):
        invoices_list = Invoice.objects.filter(pk=id)
        client = invoices_list[0].client
        t = loader.get_template('registration/email.txt')
        c = Context({
        'client': client.company,
        })
        send_mail('Welcome to My Project', t.render(c), '[email protected]', ['[email protected]'], fail_silently=False)
        return render_to_response('email.html', locals(), context_instance=RequestContext(request))

Here is my email.txt

Dear {{client}},

And when I send it to my email account I receive this

Dear Currys,

+5  A: 

This is nothing to do with send_mail. You are sending this to your template context"

c = Context({
    'invoice': 'invoice.client',
    })

Here 'invoice' is a string containing the text 'invoice.client'. You need to send an actual object. However your naming is unclear, so I can't tell whether you want to send the Invoice object, or the related Client.

Daniel Roseman
I want to to see whatever in {{invoice.client}} data to be able to be displayed in the email
Shehzad009
Well then, send `invoice.client` rather than `'invoice.client'`.
Daniel Roseman
I get an error "global name 'invoice' is not defined". I believe the invoice they are talking is in invoice.client. Not 'invoice'
Shehzad009
You *really* have to sort out your naming conventions. If you want to send the client, send the client, but don't call it 'invoice'.
Daniel Roseman
Please read above context. I have made some changes.
Shehzad009
+1  A: 

As Daniel has already pointed out.

c = Context({
    'client': 'client.company',
})

Needs to be:

c = Context({
    'client': client.company,
})

When you wrap something in '' it becomes a string, not the object it was pointing to.

Mathias Nielsen
Whoops. actually that just a typo. I have worked out that problem but I guess I did not update properly. I do it now.
Shehzad009