本文整理汇总了Python中django.template.Template方法的典型用法代码示例。如果您正苦于以下问题:Python template.Template方法的具体用法?Python template.Template怎么用?Python template.Template使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类django.template
的用法示例。
在下文中一共展示了template.Template方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: view_email_preview
# 需要导入模块: from django import template [as 别名]
# 或者: from django.template import Template [as 别名]
def view_email_preview(request, alert_type, alert_id, automation_id):
alert_email = get_object_or_404(AlertEmailTemplate, id=automation_id)
alert_type = get_object_or_404(AlertType, slug=alert_type, unit__in=request.units)
alert = get_object_or_404(Alert, pk=alert_id, alerttype__unit__in=request.units)
t = Template( alert_email.content )
email_context = build_context( alert )
email_context['details'] = {}
for k, v in alert.details.items():
email_context['details'][k] = str(v)
rendered_text = t.render( Context(email_context) )
return render(request, 'alerts/view_email_preview.html', { 'alert_type':alert_type,
'alert':alert,
'alert_email':alert_email,
'rendered_text':rendered_text })
示例2: get
# 需要导入模块: from django import template [as 别名]
# 或者: from django.template import Template [as 别名]
def get(self, request, object_id):
model_fields = [f.name for f in self.opts.fields]
fields = [f for f in request.GET['fields'].split(',') if f in model_fields]
defaults = {
"form": self.form,
"fields": fields,
"formfield_callback": self.formfield_for_dbfield,
}
form_class = modelform_factory(self.model, **defaults)
form = form_class(instance=self.org_obj)
helper = FormHelper()
helper.form_tag = False
helper.include_media = False
form.helper = helper
s = '{% load i18n crispy_forms_tags %}<form method="post" action="{{action_url}}">{% crispy form %}' + \
'<button type="submit" class="btn btn-success btn-block btn-sm">{% trans "Apply" %}</button></form>'
t = template.Template(s)
c = template.Context({'form': form, 'action_url': self.model_admin_url('patch', self.org_obj.pk)})
return HttpResponse(t.render(c))
示例3: test_preferences_cp
# 需要导入模块: from django import template [as 别名]
# 或者: from django.template import Template [as 别名]
def test_preferences_cp(self):
request = RequestFactory().get('/')
context = context_processors.preferences_cp(request)
# context should have preferences.
my_preferences = context['preferences']
# preferences should have test MyPreferences object member.
my_preferences = my_preferences.MyPreferences
self.failUnless(isinstance(my_preferences, MyPreferences),
"%s should be instance of MyPreferences." % my_preferences)
# With preferences_cp is loaded as a TEMPLATE_CONTEXT_PROCESSORS
# templates should have access to preferences object.
context_instance = RequestContext(request)
t = Template("{% if preferences %}{{ preferences }}{% endif %}")
self.failUnless(t.render(context_instance), "preferences should be \
available in template context.")
t = Template("{% if preferences.MyPreferences %}{{ \
preferences.MyPreferences }}{% endif %}")
self.failUnless(t.render(context_instance), "MyPreferences should be \
available as part of preferences var in template context.")
示例4: get_template
# 需要导入模块: from django import template [as 别名]
# 或者: from django.template import Template [as 别名]
def get_template(self, template_name, skip=None):
"""
Call self.get_template_sources() and return a Template object for
the first template matching template_name. If skip is provided, ignore
template origins in skip. This is used to avoid recursion during
template extending.
"""
tried = []
for origin in self.get_template_sources(template_name):
if skip is not None and origin in skip:
tried.append((origin, 'Skipped'))
continue
try:
contents = self.get_contents(origin)
except TemplateDoesNotExist:
tried.append((origin, 'Source does not exist'))
continue
else:
return Template(
contents, origin, origin.template_name, self.engine,
)
raise TemplateDoesNotExist(template_name, tried=tried)
示例5: test_add_to_builtins
# 需要导入模块: from django import template [as 别名]
# 或者: from django.template import Template [as 别名]
def test_add_to_builtins(self):
from compat import add_to_builtins
# Explicit import of tags
template = Template(
'{% load test_app_tags %}'
'{% my_tag %}'
)
self.assertIn('Return value of my_tag', template.render(Context({})))
# No import
with self.assertRaises(TemplateSyntaxError):
template = Template(
'{% my_tag %}'
)
template.render(Context({}))
# No import but add_to_builtins call
add_to_builtins('compat.tests.test_app.templatetags.test_app_tags')
template = Template(
'{% my_tag %}'
)
self.assertIn('Return value of my_tag', template.render(Context({})))
示例6: test_get_connection_distance
# 需要导入模块: from django import template [as 别名]
# 或者: from django.template import Template [as 别名]
def test_get_connection_distance(self):
tpl = """{%% spaceless %%}
{%% load connections %%}
{%% get_connection_distance %s foo bar as distance %%}
{{ distance }}
{%% endspaceless %%}"""
create_connection(self.r, self.foo, self.bar)
assert '1' == Template(tpl % "'user_follow'").render(Context({
'foo': self.foo,
'bar': self.bar,
}))
assert '1' == Template(tpl % "rel").render(Context({
'rel': self.r,
'foo': self.foo,
'bar': self.bar,
}))
示例7: test_connections_from_object
# 需要导入模块: from django import template [as 别名]
# 或者: from django.template import Template [as 别名]
def test_connections_from_object(self):
tpl = """{%% spaceless %%}
{%% load connections %%}
{%% connections_from_object %s foo as connections %%}
{%% for c in connections %%}{{ c.to_object.username }}, {%% endfor %%}
{%% endspaceless %%}"""
create_connection(self.r, self.foo, self.bar)
create_connection(self.r, self.foo, self.jaz)
assert 'bar, jaz,' == Template(tpl % "'user_follow'").render(Context({
'foo': self.foo,
}))
assert 'bar, jaz,' == Template(tpl % "rel").render(Context({
'rel': self.r,
'foo': self.foo,
}))
示例8: test_connections_to_object
# 需要导入模块: from django import template [as 别名]
# 或者: from django.template import Template [as 别名]
def test_connections_to_object(self):
tpl = """{%% spaceless %%}
{%% load connections %%}
{%% connections_to_object %s foo as connections %%}
{%% for c in connections %%}{{ c.from_object.username }}, {%% endfor %%}
{%% endspaceless %%}"""
create_connection(self.r, self.bar, self.foo)
create_connection(self.r, self.jaz, self.foo)
assert 'bar, jaz,' == Template(tpl % "'user_follow'").render(Context({
'foo': self.foo,
}))
assert 'bar, jaz,' == Template(tpl % "rel").render(Context({
'rel': self.r,
'foo': self.foo,
}))
示例9: test_connection_exists
# 需要导入模块: from django import template [as 别名]
# 或者: from django.template import Template [as 别名]
def test_connection_exists(self):
tpl = """{%% spaceless %%}
{%% load connections %%}
{%% connection_exists %s foo bar as has_connection %%}
{{ has_connection|yesno:'True,False' }}
{%% endspaceless %%}"""
create_connection(self.r, self.foo, self.bar)
assert 'True' == Template(tpl % "'user_follow'").render(Context({
'foo': self.foo,
'bar': self.bar,
}))
assert 'True' == Template(tpl % "rel").render(Context({
'rel': self.r,
'foo': self.foo,
'bar': self.bar,
}))
示例10: write_response
# 需要导入模块: from django import template [as 别名]
# 或者: from django.template import Template [as 别名]
def write_response(self, response):
if isinstance(response, RedirectResponse):
result = HttpResponseRedirect(response.url, status=response.status)
elif isinstance(response, OptionalJSONResponse) and isinstance(response.data, HttpResponseBase):
result = response.data
elif isinstance(response, TemplateResponse):
template_path = os.path.join(base_settings.BASE_DIR, 'templates', response.template)
with open(template_path, 'r') as file:
template = file.read()
template = template.replace('{% end %}', '{% endif %}')
context = Context(response.data)
content = Template(template).render(context)
result = HttpResponse(content, status=response.status)
else:
result = HttpResponse(response.render(), status=response.status)
for name, value in self.view.default_headers().items():
result[name] = value
for name, value in response.header_items():
result[name] = value
return result
示例11: render_template
# 需要导入模块: from django import template [as 别名]
# 或者: from django.template import Template [as 别名]
def render_template(request, order_id=None):
"""
Renders the template with this title with the current
Service Order as the context
"""
title = request.POST.get('title')
tpl = get_object_or_404(Template, title=title)
content = tpl.content
if order_id:
order = get_object_or_404(Order, pk=order_id)
content = tpl.render(order)
return HttpResponse(content)
示例12: templates
# 需要导入模块: from django import template [as 别名]
# 或者: from django.template import Template [as 别名]
def templates(request, template_id=None):
if template_id:
tpl = get_object_or_404(Template, pk=template_id)
content = tpl.content
if request.session.get('current_order_id'):
tpl = template.Template(content)
order = Order.objects.get(pk=request.session['current_order_id'])
content = tpl.render(template.Context({'order': order}))
return HttpResponse(content)
templates = Template.objects.all()
return render(request, 'notes/templates.html', {'templates': templates})
示例13: clean_content
# 需要导入模块: from django import template [as 别名]
# 或者: from django.template import Template [as 别名]
def clean_content(self):
content = self.cleaned_data.get('content')
try:
template.Template(content)
except template.TemplateSyntaxError, e:
raise forms.ValidationError(_('Syntax error in template: %s') % e)
示例14: templates
# 需要导入模块: from django import template [as 别名]
# 或者: from django.template import Template [as 别名]
def templates(self):
choices = Template.objects.all().values_list('title', flat=True)
return list(choices)
示例15: render
# 需要导入模块: from django import template [as 别名]
# 或者: from django.template import Template [as 别名]
def render(self, context):
from django import template
tpl = template.Template(self.content)
return tpl.render(template.Context({'order': context}))