本文整理匯總了Python中django.core.urlresolvers.get_script_prefix方法的典型用法代碼示例。如果您正苦於以下問題:Python urlresolvers.get_script_prefix方法的具體用法?Python urlresolvers.get_script_prefix怎麽用?Python urlresolvers.get_script_prefix使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類django.core.urlresolvers
的用法示例。
在下文中一共展示了urlresolvers.get_script_prefix方法的6個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: to_internal_value
# 需要導入模塊: from django.core import urlresolvers [as 別名]
# 或者: from django.core.urlresolvers import get_script_prefix [as 別名]
def to_internal_value(self, data):
try:
http_prefix = data.startswith(('http:', 'https:'))
except AttributeError:
self.fail('incorrect_type', data_type=type(data).__name__)
if http_prefix:
# If needed convert absolute URLs to relative path
data = urlparse(data).path
prefix = get_script_prefix()
if data.startswith(prefix):
data = '/' + data[len(prefix):]
try:
match = resolve(data)
except Resolver404:
self.fail('no_match')
try:
lookup_value = match.kwargs[self.lookup_url_kwarg]
lookup_kwargs = {self.lookup_field: lookup_value}
model = match.func.cls.model
return model.objects.get(**lookup_kwargs)
except (ObjectDoesNotExist, TypeError, ValueError):
self.fail('does_not_exist')
示例2: get_canonical_path
# 需要導入模塊: from django.core import urlresolvers [as 別名]
# 或者: from django.core.urlresolvers import get_script_prefix [as 別名]
def get_canonical_path(resource_key, pk=None):
"""
Return canonical resource path.
Arguments:
resource_key - Canonical resource key
i.e. Serializer.get_resource_key().
pk - (Optional) Object's primary key for a single-resource URL.
Returns: Absolute URL as string.
"""
if resource_key not in resource_map:
# Note: Maybe raise?
return None
base_path = get_script_prefix() + resource_map[resource_key]['path']
if pk:
return '%s/%s/' % (base_path, pk)
else:
return base_path
示例3: add_preserved_filters
# 需要導入模塊: from django.core import urlresolvers [as 別名]
# 或者: from django.core.urlresolvers import get_script_prefix [as 別名]
def add_preserved_filters(context, url, popup=False, to_field=None):
opts = context.get('opts')
preserved_filters = context.get('preserved_filters')
parsed_url = list(urlparse(url))
parsed_qs = dict(parse_qsl(parsed_url[4]))
merged_qs = dict()
if opts and preserved_filters:
preserved_filters = dict(parse_qsl(preserved_filters))
match_url = '/%s' % url.partition(get_script_prefix())[2]
try:
match = resolve(match_url)
except Resolver404:
pass
else:
current_url = '%s:%s' % (match.app_name, match.url_name)
changelist_url = 'admin:%s_%s_changelist' % (opts.app_label, opts.model_name)
if changelist_url == current_url and '_changelist_filters' in preserved_filters:
preserved_filters = dict(parse_qsl(preserved_filters['_changelist_filters']))
merged_qs.update(preserved_filters)
if popup:
from django.contrib.admin.options import IS_POPUP_VAR
merged_qs[IS_POPUP_VAR] = 1
if to_field:
from django.contrib.admin.options import TO_FIELD_VAR
merged_qs[TO_FIELD_VAR] = to_field
merged_qs.update(parsed_qs)
parsed_url[4] = urlencode(merged_qs)
return urlunparse(parsed_url)
示例4: get_absolute_url
# 需要導入模塊: from django.core import urlresolvers [as 別名]
# 或者: from django.core.urlresolvers import get_script_prefix [as 別名]
def get_absolute_url(self):
# Handle script prefix manually because we bypass reverse()
return iri_to_uri(get_script_prefix().rstrip('/') + self.url)
示例5: __init__
# 需要導入模塊: from django.core import urlresolvers [as 別名]
# 或者: from django.core.urlresolvers import get_script_prefix [as 別名]
def __init__(self, prefix):
self.prefix = prefix
self.old_prefix = get_script_prefix()
示例6: from_native
# 需要導入模塊: from django.core import urlresolvers [as 別名]
# 或者: from django.core.urlresolvers import get_script_prefix [as 別名]
def from_native(self, value):
# Convert URL -> model instance pk
# TODO: Use values_list
queryset = self.queryset
if queryset is None:
raise Exception('Writable related fields must include a `queryset` argument')
try:
http_prefix = value.startswith(('http:', 'https:'))
except AttributeError:
msg = self.error_messages['incorrect_type']
raise ValidationError(msg % type(value).__name__)
if http_prefix:
# If needed convert absolute URLs to relative path
value = urlparse.urlparse(value).path
prefix = get_script_prefix()
if value.startswith(prefix):
value = '/' + value[len(prefix):]
try:
match = resolve(value)
except Exception:
raise ValidationError(self.error_messages['no_match'])
if match.view_name != self.view_name:
raise ValidationError(self.error_messages['incorrect_match'])
try:
return self.get_object(queryset, match.view_name,
match.args, match.kwargs)
except (ObjectDoesNotExist, TypeError, ValueError):
raise ValidationError(self.error_messages['does_not_exist'])