本文整理汇总了Python中django.template.TemplateDoesNotExist方法的典型用法代码示例。如果您正苦于以下问题:Python template.TemplateDoesNotExist方法的具体用法?Python template.TemplateDoesNotExist怎么用?Python template.TemplateDoesNotExist使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类django.template
的用法示例。
在下文中一共展示了template.TemplateDoesNotExist方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: setUp
# 需要导入模块: from django import template [as 别名]
# 或者: from django.template import TemplateDoesNotExist [as 别名]
def setUp(self):
super(TestMMVHTMLRenderer, self).setUp()
"""
Monkeypatch get_template
Taken from DRF Tests
"""
self.get_template = django.template.loader.get_template
def get_template(template_name, dirs=None):
if template_name == 'test.html':
return engines['django'].from_string("<html>test: {{ data }}</html>")
raise TemplateDoesNotExist(template_name)
def select_template(template_name_list, dirs=None, using=None):
if template_name_list == ['test.html']:
return engines['django'].from_string("<html>test: {{ data }}</html>")
raise TemplateDoesNotExist(template_name_list[0])
django.template.loader.get_template = get_template
django.template.loader.select_template = select_template
示例2: render
# 需要导入模块: from django import template [as 别名]
# 或者: from django.template import TemplateDoesNotExist [as 别名]
def render(self, render_type, context):
"""
Renders the template
:param render_type: the content type to render
:param context: context data dictionary
:return: the rendered content
"""
assert render_type in self.render_types, 'Invalid Render Type'
try:
content = render_to_string('herald/{}/{}.{}'.format(
render_type,
self.template_name,
'txt' if render_type == 'text' else render_type
), context)
except TemplateDoesNotExist:
content = None
if settings.DEBUG:
raise
return content
示例3: page_not_found
# 需要导入模块: from django import template [as 别名]
# 或者: from django.template import TemplateDoesNotExist [as 别名]
def page_not_found(request, template_name='404.html'):
"""
Default 404 handler.
Templates: :template:`404.html`
Context:
request_path
The path of the requested URL (e.g., '/app/pages/bad_page/')
"""
context = {'request_path': request.path}
try:
template = loader.get_template(template_name)
body = template.render(context, request)
content_type = None # Django will use DEFAULT_CONTENT_TYPE
except TemplateDoesNotExist:
template = Engine().from_string(
'<h1>Not Found</h1>'
'<p>The requested URL {{ request_path }} was not found on this server.</p>')
body = template.render(Context(context))
content_type = 'text/html'
return http.HttpResponseNotFound(body, content_type=content_type)
示例4: bad_request
# 需要导入模块: from django import template [as 别名]
# 或者: from django.template import TemplateDoesNotExist [as 别名]
def bad_request(request, template_name='400.html'):
"""
400 error handler.
Templates: :template:`400.html`
Context: None
"""
try:
template = loader.get_template(template_name)
except TemplateDoesNotExist:
return http.HttpResponseBadRequest('<h1>Bad Request (400)</h1>', content_type='text/html')
return http.HttpResponseBadRequest(template.render())
# This can be called when CsrfViewMiddleware.process_view has not run,
# therefore need @requires_csrf_token in case the template needs
# {% csrf_token %}.
示例5: directory_index
# 需要导入模块: from django import template [as 别名]
# 或者: from django.template import TemplateDoesNotExist [as 别名]
def directory_index(path, fullpath):
try:
t = loader.select_template([
'static/directory_index.html',
'static/directory_index',
])
except TemplateDoesNotExist:
t = Engine().from_string(DEFAULT_DIRECTORY_INDEX_TEMPLATE)
files = []
for f in os.listdir(fullpath):
if not f.startswith('.'):
if os.path.isdir(os.path.join(fullpath, f)):
f += '/'
files.append(f)
c = Context({
'directory': path + '/',
'file_list': files,
})
return HttpResponse(t.render(c))
示例6: server_error
# 需要导入模块: from django import template [as 别名]
# 或者: from django.template import TemplateDoesNotExist [as 别名]
def server_error(request, template_name=ERROR_500_TEMPLATE_NAME):
"""
500 error handler.
:template: :file:`500.html`
"""
try:
template = loader.get_template(template_name)
except TemplateDoesNotExist:
if template_name != ERROR_500_TEMPLATE_NAME:
# Reraise if it's a missing custom template.
raise
return http.HttpResponseServerError(
"<h1>Server Error (500)</h1>", content_type="text/html"
)
return http.HttpResponseServerError(template.render(request=request))
# This can be called when CsrfViewMiddleware.process_view has not run,
# therefore need @requires_csrf_token in case the template needs
# {% csrf_token %}.
示例7: permission_denied
# 需要导入模块: from django import template [as 别名]
# 或者: from django.template import TemplateDoesNotExist [as 别名]
def permission_denied(request, exception, template_name=ERROR_403_TEMPLATE_NAME):
"""
Permission denied (403) handler.
:template: :file:`403.html`
If the template does not exist, an Http403 response containing the text
"403 Forbidden" (as per RFC 7231) will be returned.
"""
try:
template = loader.get_template(template_name)
except TemplateDoesNotExist:
if template_name != ERROR_403_TEMPLATE_NAME:
# Reraise if it's a missing custom template.
raise
return http.HttpResponseForbidden(
"<h1>403 Forbidden</h1>", content_type="text/html"
)
return http.HttpResponseForbidden(
template.render(request=request, context={"exception": force_text(exception)})
)
示例8: bad_request
# 需要导入模块: from django import template [as 别名]
# 或者: from django.template import TemplateDoesNotExist [as 别名]
def bad_request(request, exception, template_name=ERROR_400_TEMPLATE_NAME):
"""
400 error handler.
Templates: :template:`400.html`
Context: None
"""
try:
template = loader.get_template(template_name)
except TemplateDoesNotExist:
if template_name != ERROR_400_TEMPLATE_NAME:
# Reraise if it's a missing custom template.
raise
return HttpResponseBadRequest('<h1>Bad Request (400)</h1>', content_type='text/html')
# No exception content is passed to the template, to not disclose any sensitive information.
return HttpResponseBadRequest(template.render())
# This can be called when CsrfViewMiddleware.process_view has not run,
# therefore need @requires_csrf_token in case the template needs
# {% csrf_token %}.
示例9: permission_denied
# 需要导入模块: from django import template [as 别名]
# 或者: from django.template import TemplateDoesNotExist [as 别名]
def permission_denied(request, exception, template_name=ERROR_403_TEMPLATE_NAME):
"""
Permission denied (403) handler.
Templates: :template:`403.html`
Context: None
If the template does not exist, an Http403 response containing the text
"403 Forbidden" (as per RFC 7231) will be returned.
"""
try:
template = loader.get_template(template_name)
except TemplateDoesNotExist:
if template_name != ERROR_403_TEMPLATE_NAME:
# Reraise if it's a missing custom template.
raise
return HttpResponseForbidden('<h1>403 Forbidden</h1>', content_type='text/html')
return HttpResponseForbidden(
template.render(request=request, context={'exception': str(exception)})
)
示例10: directory_index
# 需要导入模块: from django import template [as 别名]
# 或者: from django.template import TemplateDoesNotExist [as 别名]
def directory_index(path, fullpath):
try:
t = loader.select_template([
'static/directory_index.html',
'static/directory_index',
])
except TemplateDoesNotExist:
t = Engine(libraries={'i18n': 'django.templatetags.i18n'}).from_string(DEFAULT_DIRECTORY_INDEX_TEMPLATE)
c = Context()
else:
c = {}
files = []
for f in os.listdir(fullpath):
if not f.startswith('.'):
if os.path.isdir(os.path.join(fullpath, f)):
f += '/'
files.append(f)
c.update({
'directory': path + '/',
'file_list': files,
})
return HttpResponse(t.render(c))
示例11: get_template
# 需要导入模块: from django import template [as 别名]
# 或者: from django.template import TemplateDoesNotExist [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)
示例12: entry_point
# 需要导入模块: from django import template [as 别名]
# 或者: from django.template import TemplateDoesNotExist [as 别名]
def entry_point(context, block_name):
"""include an snippet at the bottom of a block, if it exists
For example, if the plugin with slug 'attachments' is registered
waliki/attachments_edit_content.html will be included with
{% entry_point 'edit_content' %}
which is declared at the bottom of the block 'content' in edit.html
"""
from waliki.plugins import get_plugins
includes = []
for plugin in get_plugins():
template_name = 'waliki/%s_%s.html' % (plugin.slug, block_name)
try:
# template exists
template.loader.get_template(template_name)
includes.append(template_name)
except template.TemplateDoesNotExist:
continue
context.update({'includes': includes})
return context
示例13: approval_status_changed
# 需要导入模块: from django import template [as 别名]
# 或者: from django.template import TemplateDoesNotExist [as 别名]
def approval_status_changed(obj, request, **kwargs):
get_recipients = {
obj.PENDING: obj.get_approver_email_list,
obj.WITHDRAWN: obj.get_approver_email_list,
obj.APPROVED: obj.get_submitter_email_list,
obj.REJECTED: obj.get_submitter_email_list,
}[obj.approval_status]
# produces template names like "home/email/project-pending.txt"
template = "{}/email/{}-{}.txt".format(
obj._meta.app_label,
obj._meta.model_name,
obj.get_approval_status_display().lower())
try:
send_template_mail(template, {
obj._meta.model_name: obj,
},
request=request,
recipient_list=get_recipients(),
**kwargs)
except TemplateDoesNotExist:
logger.info(
"not sending approval status change email because %s does not exist",
template, exc_info=True)
示例14: show_includes
# 需要导入模块: from django import template [as 别名]
# 或者: from django.template import TemplateDoesNotExist [as 别名]
def show_includes(template_path, seen, depth=0):
if template_path in seen:
return
print(" " * depth + template_path)
seen.add(template_path)
try:
template = loader.get_template(template_path).template
except TemplateDoesNotExist:
print(" " * depth + "*** previous template not found!")
return
for included in re.findall(r"{%\s*include\s+([^\s]*)[^%]*%}", template.source):
if included[0] in "\"'":
show_includes(included[1:-1], seen, depth + 1)
else:
print(" " * (depth + 1) + "*** template name given by expression:", repr(included))
示例15: directory_index
# 需要导入模块: from django import template [as 别名]
# 或者: from django.template import TemplateDoesNotExist [as 别名]
def directory_index(path, fullpath):
try:
t = loader.select_template([
'static/directory_index.html',
'static/directory_index',
])
except TemplateDoesNotExist:
t = Engine(libraries={'i18n': 'django.templatetags.i18n'}).from_string(DEFAULT_DIRECTORY_INDEX_TEMPLATE)
c = Context()
else:
c = {}
files = []
for f in fullpath.iterdir():
if not f.name.startswith('.'):
url = str(f.relative_to(fullpath))
if f.is_dir():
url += '/'
files.append(url)
c.update({
'directory': path + '/',
'file_list': files,
})
return HttpResponse(t.render(c))