Replacing text with variables

Posted by Steve on Stack Overflow See other posts from Stack Overflow or by Steve
Published on 2011-11-16T18:07:38Z Indexed on 2011/11/17 17:51 UTC
Read the original article Hit count: 137

I have to send out letters to certain clients and I have a standard letter that I need to use. I want to replace some of the text inside the body of the message with variables.

Here is my maturity_letter models.py

class MaturityLetter(models.Model):
default = models.BooleanField(default=False, blank=True)
body = models.TextField(blank=True)
footer = models.TextField(blank=True)

Now the body has a value of this:

Dear [primary-firstname],

AN IMPORTANT REMINDER…

You have a [product] that is maturing on [maturity_date] with [financial institution].

etc

Now I would like to replace everything in brackets with my template variables.

This is what I have in my views.py so far:

context = {}
if request.POST:
    start_form = MaturityLetterSetupForm(request.POST)
    if start_form.is_valid():
        agent = request.session['agent']
        start_date = start_form.cleaned_data['start_date']
        end_date = start_form.cleaned_data['end_date']
        investments = Investment.objects.all().filter(maturity_date__range=(start_date, end_date), plan__profile__agent=agent).order_by('maturity_date')
        inv_form = MaturityLetterInvestments(investments, request.POST)
        if inv_form.is_valid():
            sel_inv = inv_form.cleaned_data['investments']
            context['sel_inv'] = sel_inv
        maturity_letter = MaturityLetter.objects.get(id=1)

        context['mat_letter'] = maturity_letter
        context['inv_form'] = inv_form
        context['agent'] = agent
        context['show_report'] = True

Now if I loop through the sel_inv I get access to sel_inv.maturity_date, etc but I am lost in how to replace the text.

On my template, all I have so far is:

{% if show_letter %}
{{ mat_letter.body }} <br/>
{{ mat_letter.footer }}
{% endif %}

Much appreciated.

© Stack Overflow or respective owner

Related posts about python

Related posts about django