本文整理匯總了Python中django.utils.encoding.force_text方法的典型用法代碼示例。如果您正苦於以下問題:Python encoding.force_text方法的具體用法?Python encoding.force_text怎麽用?Python encoding.force_text使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類django.utils.encoding
的用法示例。
在下文中一共展示了encoding.force_text方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: render_option
# 需要導入模塊: from django.utils import encoding [as 別名]
# 或者: from django.utils.encoding import force_text [as 別名]
def render_option(self, selected_choices, option_value, option_label):
if option_value is None:
option_value = ''
option_value = force_text(option_value)
if option_value in selected_choices:
selected_html = mark_safe(' selected="selected"')
if not self.allow_multiple_selected:
# Only allow for a single selection.
selected_choices.remove(option_value)
else:
selected_html = ''
return format_html('<option data-icon="{0}" value="{0}"{1}>{2}</option>',
option_value,
selected_html,
force_text(option_label),
)
示例2: _get_cache_key
# 需要導入模塊: from django.utils import encoding [as 別名]
# 或者: from django.utils.encoding import force_text [as 別名]
def _get_cache_key(self, request):
"""
Generate cache key that's exactly unique enough.
Assumes that the response is determined by the request.method, authenticated user, and URL path.
"""
# HTTP method
method = request.method
# Authenticated username
if not request.user.is_authenticated or self.cache_ignore_auth:
username = '*'
else:
username = request.user.username
# URL path
url = force_text(iri_to_uri(request.get_full_path()))
# build a cache key out of that
key = '#'.join(('CacheMixin', self.key_prefix, username, method, url))
if len(key) > MAX_KEY_LENGTH:
# make sure keys don't get too long
key = key[:(MAX_KEY_LENGTH - 33)] + '-' + hashlib.md5(key.encode('utf8')).hexdigest()
return key
示例3: render
# 需要導入模塊: from django.utils import encoding [as 別名]
# 或者: from django.utils.encoding import force_text [as 別名]
def render(self, name, value, attrs=None):
if value is None:
value = ''
if DJANGO_11:
final_attrs = self.build_attrs(attrs, extra_attrs={'name': name})
else:
final_attrs = self.build_attrs(attrs, name=name)
final_attrs['class'] = 'nav nav-pills nav-stacked'
output = [u'<ul%s>' % flatatt(final_attrs)]
options = self.render_options(force_text(value), final_attrs['id'])
if options:
output.append(options)
output.append(u'</ul>')
output.append('<input type="hidden" id="%s_input" name="%s" value="%s"/>' %
(final_attrs['id'], name, force_text(value)))
return mark_safe(u'\n'.join(output))
示例4: get_context
# 需要導入模塊: from django.utils import encoding [as 別名]
# 或者: from django.utils.encoding import force_text [as 別名]
def get_context(self):
"""
Prepare the context for templates.
"""
self.title = _('%s List') % force_text(self.opts.verbose_name)
model_fields = [(f, f.name in self.list_display, self.get_check_field_url(f))
for f in (list(self.opts.fields) + self.get_model_method_fields()) if f.name not in self.list_exclude]
new_context = {
'model_name': force_text(self.opts.verbose_name_plural),
'title': self.title,
'cl': self,
'model_fields': model_fields,
'clean_select_field_url': self.get_query_string(remove=[COL_LIST_VAR]),
'has_add_permission': self.has_add_permission(),
'app_label': self.app_label,
'brand_name': self.opts.verbose_name_plural,
'brand_icon': self.get_model_icon(self.model),
'add_url': self.model_admin_url('add'),
'result_headers': self.result_headers(),
'results': self.results()
}
context = super(ListAdminView, self).get_context()
context.update(new_context)
return context
示例5: init_request
# 需要導入模塊: from django.utils import encoding [as 別名]
# 或者: from django.utils.encoding import force_text [as 別名]
def init_request(self, object_id, *args, **kwargs):
"The 'delete' admin view for this model."
self.obj = self.get_object(unquote(object_id))
if not self.has_delete_permission(self.obj):
raise PermissionDenied
if self.obj is None:
raise Http404(_('%(name)s object with primary key %(key)r does not exist.') % {'name': force_text(self.opts.verbose_name), 'key': escape(object_id)})
using = router.db_for_write(self.model)
# Populate deleted_objects, a data structure of all related objects that
# will also be deleted.
(self.deleted_objects, model_count, self.perms_needed, self.protected) = get_deleted_objects(
[self.obj], self.opts, self.request.user, self.admin_site, using)
示例6: get_context
# 需要導入模塊: from django.utils import encoding [as 別名]
# 或者: from django.utils.encoding import force_text [as 別名]
def get_context(self):
if self.perms_needed or self.protected:
title = _("Cannot delete %(name)s") % {"name":
force_text(self.opts.verbose_name)}
else:
title = _("Are you sure?")
new_context = {
"title": title,
"object": self.obj,
"deleted_objects": self.deleted_objects,
"perms_lacking": self.perms_needed,
"protected": self.protected,
}
context = super(DeleteAdminView, self).get_context()
context.update(new_context)
return context
示例7: get_context
# 需要導入模塊: from django.utils import encoding [as 別名]
# 或者: from django.utils.encoding import force_text [as 別名]
def get_context(self):
new_context = {
'title': _('%s Detail') % force_text(self.opts.verbose_name),
'form': self.form_obj,
'object': self.obj,
'has_change_permission': self.has_change_permission(self.obj),
'has_delete_permission': self.has_delete_permission(self.obj),
'content_type_id': ContentType.objects.get_for_model(self.model).id,
}
context = super(DetailAdminView, self).get_context()
context.update(new_context)
return context
示例8: test_create_order_with_basket_shipping_option
# 需要導入模塊: from django.utils import encoding [as 別名]
# 或者: from django.utils.encoding import force_text [as 別名]
def test_create_order_with_basket_shipping_option(self):
amount = 11
rate = ShippingRate.objects.create(
name=force_text(uuid.uuid4()),
rate=amount,
carrier=force_text(uuid.uuid4()),
description=force_text(uuid.uuid4()),
basket_id=self.basket_id,
)
order = create_order(
self.email,
self.request,
shipping_address=self.shipping_address,
billing_address=self.billing_address,
shipping_option=rate.name,
)
self.assertEqual(order.shipping_rate, amount)
示例9: test_create_order_with_address_shipping_option
# 需要導入模塊: from django.utils import encoding [as 別名]
# 或者: from django.utils.encoding import force_text [as 別名]
def test_create_order_with_address_shipping_option(self):
amount = 12
rate = ShippingRate.objects.create(
name=force_text(uuid.uuid4()),
rate=amount,
carrier=force_text(uuid.uuid4()),
description=force_text(uuid.uuid4()),
destination=self.shipping_address,
)
order = create_order(
self.email,
self.request,
shipping_address=self.shipping_address,
billing_address=self.billing_address,
shipping_option=rate.name,
)
self.assertEqual(order.shipping_rate, amount)
示例10: render_options
# 需要導入模塊: from django.utils import encoding [as 別名]
# 或者: from django.utils.encoding import force_text [as 別名]
def render_options(self, *args):
"""Render only selected options and set QuerySet from :class:`ModelChoiceIterator`."""
try:
selected_choices, = args
except ValueError:
choices, selected_choices = args
choices = chain(self.choices, choices)
else:
choices = self.choices
selected_choices = {force_text(v) for v in selected_choices}
output = ['<option></option>' if not self.is_required and not self.allow_multiple_selected else '']
if isinstance(self.choices, ModelChoiceIterator):
if self.queryset is None:
self.queryset = self.choices.queryset
selected_choices = {c for c in selected_choices
if c not in self.choices.field.empty_values}
choices = [(obj.pk, self.label_from_instance(obj))
for obj in self.choices.queryset.filter(pk__in=selected_choices)]
else:
choices = [(k, v) for k, v in choices if force_text(k) in selected_choices]
for option_value, option_label in choices:
output.append(self.render_option(selected_choices, option_value, option_label))
return '\n'.join(output)
示例11: label_from_instance
# 需要導入模塊: from django.utils import encoding [as 別名]
# 或者: from django.utils.encoding import force_text [as 別名]
def label_from_instance(self, obj):
"""
Return option label representation from instance.
Can be overridden to change the representation of each choice.
Example usage::
class MyWidget(ModelSelect2Widget):
def label_from_instance(obj):
return force_text(obj.title).upper()
Args:
obj (django.db.models.Model): Instance of Django Model.
Returns:
str: Option label.
"""
return force_text(obj)
示例12: make_rack_statistics
# 需要導入模塊: from django.utils import encoding [as 別名]
# 或者: from django.utils.encoding import force_text [as 別名]
def make_rack_statistics(self):
data = []
robjects = Rack.objects.filter(onidc_id=self.onidc_id, actived=True)
keys = Option.objects.filter(
flag__in=['Rack-Style', 'Rack-Status'],
actived=True)
keys = shared_queryset(keys, self.onidc_id)
for k in keys:
d = []
query = {
k.flag.split('-')[1].lower(): k
}
c = robjects.filter(**query).count()
if c > 0:
d.append(force_text(k))
d.append(c)
if d:
data.append(d)
return data
示例13: make_online_statistics
# 需要導入模塊: from django.utils import encoding [as 別名]
# 或者: from django.utils.encoding import force_text [as 別名]
def make_online_statistics(self):
data = []
dobjects = Online.objects.filter(onidc_id=self.onidc_id)
keys = Option.objects.filter(flag__in=['Device-Style', 'Device-Tags'])
keys = shared_queryset(keys, self.onidc_id)
for k in keys:
d = []
if k.flag == 'Device-Style':
c = dobjects.filter(style=k).count()
else:
c = dobjects.filter(tags__in=[k]).count()
if c > 0:
d.append(force_text(k))
d.append(c)
if d:
data.append(d)
return data
示例14: send_and_save
# 需要導入模塊: from django.utils import encoding [as 別名]
# 或者: from django.utils.encoding import force_text [as 別名]
def send_and_save(self, user):
"""
The main entry point to the sending logic
"""
from django.utils.encoding import force_text
messages = list()
recipients = [r.strip() for r in self.recipient.split(',')]
for r in recipients:
try:
messages.append(self.send_sms(r, user))
except (ValidationError, IntegrityError), e:
pass
示例15: _get_json
# 需要導入模塊: from django.utils import encoding [as 別名]
# 或者: from django.utils.encoding import force_text [as 別名]
def _get_json(self, response):
return json.loads(force_text(response.content))