나는이 람다 함수 (렌더링)에 내 머리를 감쌀 수 없다. 그것은 메 자닌 폴더의 유틸리티에서이다 : 나는 함수의 내 자신의 버전으로 템플릿 목록을 전달하기 위해 노력하고있어mezzaninie utils의 Python lambda
def send_mail_template(subject, template, addr_from, addr_to, context=None,
attachments=None, fail_silently=None, addr_bcc=None,
headers=None):
"""
Send email rendering text and html versions for the specified
template name using the context dictionary passed in.
EDITED FOR SIMPLICITY
"""
context.update(context_settings())
# Loads a template passing in vars as context.
render = lambda type: loader.get_template("%s.%s" %
(template, type)).render(Context(context))
# Create and send email.
msg = EmailMultiAlternatives(subject, render("txt"),
addr_from, addr_to, addr_bcc,
headers=headers)
msg.attach_alternative(render("html"), "text/html")
msg.send(fail_silently=fail_silently)
. 그래서 템플리트에 대한 경로의 문자열 인 "template"매개 변수는 경로 문자열의 목록 (템플리트)이됩니다. 그럼 난 목록을 반복하고 람다에서 일어나는 일을 적용하고, 그 다음에 .render (Context (context))를 호출해야합니다.
이 이제까지 추한 코드 그러나 이것은 내가 최종 결과로 필요한 보여줍니다
render = lambda type: loader.get_template("%s.%s" %
(templates[0], type))
render2 = lambda type: loader.get_template("%s.%s" %
(templates[1], type))
# Create and send email.
msg = EmailMultiAlternatives(subject,(render("txt")+render2('txt')).render(Context(context)),
addr_from, addr_to, addr_bcc,
headers=headers)
msg.attach_alternative((render("html")+render2('html').render(Context(context)), "text/html")
msg.send(fail_silently=fail_silently)
위의 작업을 수행하지만 분명히 단지 구역질 및 목록은 알 수없는 길이 될 것입니다.
주세요! 누구든지 람다 함수를 생성 할 수 있습니까?
Answer with help from Arthur Tacca
내가 함께 템플릿을 적층하여 이메일 몸을 만들려고, 그래서 하나로서 함께 TXT 및 HTML 본문으로 렌더링되는 템플릿 목록에 전달해야 해요. 여기서 일하는 작업은 다음과 같습니다
rendered = loader.get_template("%s.%s" % (template, "txt")).render(Context(context))
내가에만이 단순화를 추측 :
def render_templates(types,templates,context):
template_body = ''
for template in templates:
template_body += loader.get_template("%s.%s" % (template, types)).render(Context(context))
return template_body
def send_mail_template(subject, templates, addr_from, addr_to, context=None,
attachments=None, fail_silently=None, addr_bcc=None,
headers=None):
"""
...
"""
context.update(context_settings())
msg = EmailMultiAlternatives(subject, render_templates("txt",templates,context),
addr_from, to, [],
headers)
msg.attach_alternative(render_templates("html",templates,context), "text/html")
msg.send(fail_silently=fail_silently)
전 묻고 있었지만 당신의 대답으로 작동하도록했습니다. 많은 감사. 편집 된 질문보기 – joeskru