本文整理汇总了Python中django.utils.html.escapejs方法的典型用法代码示例。如果您正苦于以下问题:Python html.escapejs方法的具体用法?Python html.escapejs怎么用?Python html.escapejs使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类django.utils.html
的用法示例。
在下文中一共展示了html.escapejs方法的7个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: doc_html
# 需要导入模块: from django.utils import html [as 别名]
# 或者: from django.utils.html import escapejs [as 别名]
def doc_html(request, data):
with resolve('doc.html').open('rt', encoding='utf-8') as f:
html = f.read()
data_json = escapejs(json.dumps(data))
html = html.replace(
'/* HOOVER HYDRATION PLACEHOLDER */',
'window.HOOVER_HYDRATE_DOC = JSON.parse(\'{}\')'.format(data_json),
)
scripts = ''
if settings.HOOVER_HYPOTHESIS_EMBED_URL:
url = settings.HOOVER_HYPOTHESIS_EMBED_URL
scripts += '<script src="{}"></script>'.format(url)
html = html.replace('<!-- HOOVER SCRIPT PLACEHOLDER -->', scripts)
return HttpResponse(html)
示例2: render_props
# 需要导入模块: from django.utils import html [as 别名]
# 或者: from django.utils.html import escapejs [as 别名]
def render_props(self):
if self.props:
encoded = escapejs(self.json_encoder(self.props))
return mark_safe("JSON.parse('{0}')".format(encoded))
return '{}'
示例3: test_add_lazy_safe_text_and_safe_text
# 需要导入模块: from django.utils import html [as 别名]
# 或者: from django.utils.html import escapejs [as 别名]
def test_add_lazy_safe_text_and_safe_text(self):
s = html.escape(lazystr('a'))
s += mark_safe('&b')
self.assertRenderEqual('{{ s }}', 'a&b', s=s)
s = html.escapejs(lazystr('a'))
s += mark_safe('&b')
self.assertRenderEqual('{{ s }}', 'a&b', s=s)
s = text.slugify(lazystr('a'))
s += mark_safe('&b')
self.assertRenderEqual('{{ s }}', 'a&b', s=s)
示例4: test_add_lazy_safe_text_and_safe_text
# 需要导入模块: from django.utils import html [as 别名]
# 或者: from django.utils.html import escapejs [as 别名]
def test_add_lazy_safe_text_and_safe_text(self):
s = html.escape(lazystr('a'))
s += mark_safe('&b')
self.assertRenderEqual('{{ s }}', 'a&b', s=s)
s = html.escapejs(lazystr('a'))
s += mark_safe('&b')
self.assertRenderEqual('{{ s }}', 'a&b', s=s)
示例5: test_json_on_page
# 需要导入模块: from django.utils import html [as 别名]
# 或者: from django.utils.html import escapejs [as 别名]
def test_json_on_page(self):
"""Test that the JSON object is actually on the resulting page"""
self.create_thread()
response = self.client.get(reverse('threads'))
escaped_json = escapejs(response.context['js_context'])
self.assertContains(
response,
'JSON.parse("{escaped_json}")'.format(escaped_json=escaped_json)
)
示例6: response_add
# 需要导入模块: from django.utils import html [as 别名]
# 或者: from django.utils.html import escapejs [as 别名]
def response_add(self, request, obj, post_url_continue=None):
"""
Determines the HttpResponse for the add_view stage.
"""
opts = obj._meta
pk_value = obj._get_pk_val()
preserved_filters = self.get_preserved_filters(request)
msg_dict = {'name': force_text(opts.verbose_name), 'obj': force_text(obj)}
# Here, we distinguish between different save types by checking for
# the presence of keys in request.POST.
if IS_POPUP_VAR in request.POST:
to_field = request.POST.get(TO_FIELD_VAR)
if to_field:
attr = str(to_field)
else:
attr = obj._meta.pk.attname
value = obj.serializable_value(attr)
return SimpleTemplateResponse('admin/popup_response.html', {
'pk_value': escape(pk_value), # for possible backwards-compatibility
'value': escape(value),
'obj': escapejs(obj)
})
elif "_continue" in request.POST:
msg = _('The %(name)s "%(obj)s" was added successfully. You may edit it again below.') % msg_dict
self.message_user(request, msg, messages.SUCCESS)
if post_url_continue is None:
post_url_continue = reverse('admin:%s_%s_change' %
(opts.app_label, opts.model_name),
args=(quote(pk_value),),
current_app=self.admin_site.name)
post_url_continue = add_preserved_filters(
{'preserved_filters': preserved_filters, 'opts': opts},
post_url_continue
)
return HttpResponseRedirect(post_url_continue)
elif "_addanother" in request.POST:
msg = _('The %(name)s "%(obj)s" was added successfully. You may add another %(name)s below.') % msg_dict
self.message_user(request, msg, messages.SUCCESS)
redirect_url = request.path
redirect_url = add_preserved_filters({'preserved_filters': preserved_filters, 'opts': opts}, redirect_url)
return HttpResponseRedirect(redirect_url)
else:
msg = _('The %(name)s "%(obj)s" was added successfully.') % msg_dict
self.message_user(request, msg, messages.SUCCESS)
return self.response_post_save_add(request, obj)
示例7: response_change
# 需要导入模块: from django.utils import html [as 别名]
# 或者: from django.utils.html import escapejs [as 别名]
def response_change(self, request, obj):
"""
Determines the HttpResponse for the change_view stage.
"""
if IS_POPUP_VAR in request.POST:
to_field = request.POST.get(TO_FIELD_VAR)
attr = str(to_field) if to_field else obj._meta.pk.attname
# Retrieve the `object_id` from the resolved pattern arguments.
value = request.resolver_match.args[0]
new_value = obj.serializable_value(attr)
return SimpleTemplateResponse('admin/popup_response.html', {
'action': 'change',
'value': escape(value),
'obj': escapejs(obj),
'new_value': escape(new_value),
})
opts = self.model._meta
pk_value = obj._get_pk_val()
preserved_filters = self.get_preserved_filters(request)
msg_dict = {'name': force_text(opts.verbose_name), 'obj': force_text(obj)}
if "_continue" in request.POST:
msg = _('The %(name)s "%(obj)s" was changed successfully. You may edit it again below.') % msg_dict
self.message_user(request, msg, messages.SUCCESS)
redirect_url = request.path
redirect_url = add_preserved_filters({'preserved_filters': preserved_filters, 'opts': opts}, redirect_url)
return HttpResponseRedirect(redirect_url)
elif "_saveasnew" in request.POST:
msg = _('The %(name)s "%(obj)s" was added successfully. You may edit it again below.') % msg_dict
self.message_user(request, msg, messages.SUCCESS)
redirect_url = reverse('admin:%s_%s_change' %
(opts.app_label, opts.model_name),
args=(pk_value,),
current_app=self.admin_site.name)
redirect_url = add_preserved_filters({'preserved_filters': preserved_filters, 'opts': opts}, redirect_url)
return HttpResponseRedirect(redirect_url)
elif "_addanother" in request.POST:
msg = _('The %(name)s "%(obj)s" was changed successfully. You may add another %(name)s below.') % msg_dict
self.message_user(request, msg, messages.SUCCESS)
redirect_url = reverse('admin:%s_%s_add' %
(opts.app_label, opts.model_name),
current_app=self.admin_site.name)
redirect_url = add_preserved_filters({'preserved_filters': preserved_filters, 'opts': opts}, redirect_url)
return HttpResponseRedirect(redirect_url)
else:
msg = _('The %(name)s "%(obj)s" was changed successfully.') % msg_dict
self.message_user(request, msg, messages.SUCCESS)
return self.response_post_save_change(request, obj)