def _send_mass_contact_email(ticket, email_subject, email_body):
template = loader.get_template_from_string(email_subject)
context = Context({
'publicId': ticket.publicId,
'service': ticket.service.name.replace('.', '[.]'),
'lang': ticket.defendant.details.lang,
})
subject = template.render(context)
template = loader.get_template_from_string(email_body)
context = Context({
'publicId': ticket.publicId,
'service': ticket.service.name.replace('.', '[.]'),
'lang': ticket.defendant.details.lang,
})
body = template.render(context)
implementations.instance.get_singleton_of('MailerServiceBase').send_email(
ticket,
ticket.defendant.details.email,
subject,
body,
'MassContact',
)
python类get_template_from_string()的实例源码
def test_field_type_hidden(self):
template = loader.get_template_from_string(u"""
{% load crispy_forms_tags %}
{% crispy test_form %}
""")
test_form = TestForm()
test_form.helper = FormHelper()
test_form.helper.layout = Layout(
Field('email', type="hidden", data_test=12),
Field('datetime_field'),
)
c = Context({
'test_form': test_form,
})
html = template.render(c)
# Check form parameters
self.assertEqual(html.count('data-test="12"'), 1)
self.assertEqual(html.count('name="email"'), 1)
self.assertEqual(html.count('class="dateinput'), 1)
self.assertEqual(html.count('class="timeinput'), 1)
def _get_template(template_string):
if __is_18:
return engines['django'].from_string(template_string)
else:
return loader.get_template_from_string(template_string)
def _get_template(template_string):
if __is_18:
return engines['django'].from_string(template_string)
else:
return loader.get_template_from_string(template_string)
def test_lorem_tag(self):
t = loader.get_template_from_string("{% load webdesign %}{% lorem 3 w %}")
self.assertEqual(t.render(Context({})),
'lorem ipsum dolor')
def template_from_string(template_code):
if Engine:
return Engine().from_string(template_code)
else: # Django < 1.8
return get_template_from_string(template_code)
def test_multiwidget_field(self):
template = loader.get_template_from_string(u"""
{% load crispy_forms_tags %}
{% crispy form %}
""")
test_form = TestForm()
test_form.helper = FormHelper()
test_form.helper.layout = Layout(
MultiWidgetField(
'datetime_field',
attrs=(
{'rel': 'test_dateinput'},
{'rel': 'test_timeinput', 'style': 'width: 30px;', 'type': "hidden"}
)
)
)
c = Context({'form': test_form})
html = template.render(c)
self.assertEqual(html.count('class="dateinput'), 1)
self.assertEqual(html.count('rel="test_dateinput"'), 1)
self.assertEqual(html.count('rel="test_timeinput"'), 1)
self.assertEqual(html.count('style="width: 30px;"'), 1)
self.assertEqual(html.count('type="hidden"'), 1)
def test_as_crispy_errors_form_without_non_field_errors(self):
template = loader.get_template_from_string(u"""
{% load crispy_forms_tags %}
{{ form|as_crispy_errors }}
""")
form = TestForm({'password1': "god", 'password2': "god"})
form.is_valid()
c = Context({'form': form})
html = template.render(c)
self.assertFalse("errorMsg" in html or "alert" in html)
def test_as_crispy_errors_form_with_non_field_errors(self):
template = loader.get_template_from_string(u"""
{% load crispy_forms_tags %}
{{ form|as_crispy_errors }}
""")
form = TestForm({'password1': "god", 'password2': "wargame"})
form.is_valid()
c = Context({'form': form})
html = template.render(c)
self.assertTrue("errorMsg" in html or "alert" in html)
self.assertTrue("<li>Passwords dont match</li>" in html)
self.assertFalse("<h3>" in html)
def test_crispy_filter_with_form(self):
template = loader.get_template_from_string(u"""
{% load crispy_forms_tags %}
{{ form|crispy }}
""")
c = Context({'form': TestForm()})
html = template.render(c)
self.assertTrue("<td>" not in html)
self.assertTrue("id_is_company" in html)
self.assertEqual(html.count('<label'), 7)
def test_classes_filter(self):
template = loader.get_template_from_string(u"""
{% load crispy_forms_field %}
{{ testField|classes }}
""")
test_form = TestForm()
test_form.fields['email'].widget.attrs.update({'class': 'email-fields'})
c = Context({'testField': test_form.fields['email']})
html = template.render(c)
self.assertTrue('email-fields' in html)
def test_crispy_field_and_class_converters(self):
if hasattr(settings, 'CRISPY_CLASS_CONVERTERS'):
template = loader.get_template_from_string(u"""
{% load crispy_forms_field %}
{% crispy_field testField 'class' 'error' %}
""")
test_form = TestForm()
field_instance = test_form.fields['email']
bound_field = BoundField(test_form, field_instance, 'email')
c = Context({'testField': bound_field})
html = template.render(c)
self.assertTrue('error' in html)
self.assertTrue('inputtext' in html)