本文整理汇总了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'])