开发者

Using send_mail in Django: Cannot display data in invoice from functions

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开发者_Go百科?

    #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), 'jess@example.com', ['mark@example.com'], 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,


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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜