用变量替换文本 [英] Replacing text with variables

查看:129
本文介绍了用变量替换文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我必须向某些客户发送信件,并且我有一个我需要使用的标准信件。我想使用变量替换消息正文内的一些文本。



这是我的maturity_letter models.py



$ $ $ $ $ $ $ $ $ $ $ $ $ $
default = models.BooleanField(default = False,blank = True)
body = models.TextField(blank = true)
footer = models.TextField(blank = True)

一个值:


尊敬的[primary-firstname],



重要提示...



您有[金融机构] [[maturity_date])已经成熟的[产品]。



etc


现在我想替换括号中的所有模板变量。



这是迄今为止在我的views.py中有的:

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

上下文[ 'mat_letter'] = maturity_letter
上下文['inv_form'] = inv_form
上下文['agent'] =代理
上下文['show_report'] = True

现在,如果我循环访问 sel_inv ,我可以访问 sel_inv.maturity_date 等等,但我迷失在如何复制



在我的模板上,我到目前为止都是:

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

赞赏。

解决方案

我认为这是最好的方法。首先,你有一个包含你的模板的文件,例如:

 尊敬的{{primary-firstname}},
重要提示...
{{financial {} {} {} {} {{{
etc ...

所以,你的观点会像:

  from django.template.loader import render_to_string 

#previous code ...
template_file ='where /is/my/template.txt'
context_data = {'primary-firstname':'先生约翰逊,
'产品':'香蕉',
'maturity_date':'11 -17-2011',
'金融机构':'别的'}
message = render_to_string(template_file,context_data)
#这里你发送消息给用户...

所以如果你打印消息,你会得到:

 亲爱的约翰逊先生,
一个重要提示...
你有一个香蕉正在成熟11-17-2011与别的东西。
等...


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.

解决方案

I think this is the best way to do it. First, you have a file with your template, something like:

Dear {{primary-firstname}},
AN IMPORTANT REMINDER…
You have a {{product}} that is maturing on {{maturity_date}} with {{financial institution}}.
etc ...

So, your view will go something like:

from django.template.loader import render_to_string

# previous code ...
template_file = 'where/is/my/template.txt'
context_data = {'primary-firstname': 'Mr. Johnson',
                'product': 'banana',
                'maturity_date': '11-17-2011',
                'financial institution': 'something else'}
message = render_to_string(template_file, context_data)
# here you send the message to the user ...

So if you print message you'll get:

Dear Mr. Johnson,
AN IMPORTANT REMINDER…
You have a banana that is maturing on 11-17-2011 with something else.
etc ...

这篇关于用变量替换文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆