本文整理汇总了Python中django.http.request.HttpRequest.META["SERVER_NAME"]方法的典型用法代码示例。如果您正苦于以下问题:Python HttpRequest.META["SERVER_NAME"]方法的具体用法?Python HttpRequest.META["SERVER_NAME"]怎么用?Python HttpRequest.META["SERVER_NAME"]使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类django.http.request.HttpRequest
的用法示例。
在下文中一共展示了HttpRequest.META["SERVER_NAME"]方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: set_choices_via_ajax
# 需要导入模块: from django.http.request import HttpRequest [as 别名]
# 或者: from django.http.request.HttpRequest import META["SERVER_NAME"] [as 别名]
def set_choices_via_ajax(self, kwargs=None, is_initial=False):
for field_name in self.chained_fields_names + self.chained_model_fields_names:
field = self.fields[field_name]
try:
if kwargs is not None:
# initial data do not have any prefix
if self.prefix in EMPTY_VALUES or is_initial:
parent_value = kwargs.get(field.parent_field, None)
field_value = kwargs.get(field_name, None)
else:
parent_value = kwargs.get('%s-%s' % (self.prefix, field.parent_field), None)
field_value = kwargs.get('%s-%s' % (self.prefix, field_name), None)
else:
parent_value = self.initial.get(field.parent_field, None)
field_value = self.initial.get(field_name, None)
if parent_value is None:
parent_value = getattr(self, '%s' % field.parent_field, None)
if field_value is None:
field_value = getattr(self, '%s' % field_name, None)
field.choices = [('', field.empty_label)]
# check that parent_value is valid
if parent_value:
parent_value = getattr(parent_value, 'pk', parent_value)
url = force_text(field.ajax_url)
params = {
'field_name': field_name,
'parent_value': parent_value,
'field_value': field_value
}
# This will get the callable from the url.
# All we need to do is pass in a 'request'
url_callable = resolve(url).func
# Build the fake request
fake_request = HttpRequest()
fake_request.META["SERVER_NAME"] = "localhost"
fake_request.META["SERVER_PORT"] = '80'
# Add parameters and user if supplied
fake_request.method = "GET"
for key, value in params.items():
fake_request.GET[key] = value
if hasattr(self, "user") and self.user:
fake_request.user = self.user
else:
fake_request.user = AnonymousUser()
# Get the response
response = url_callable(fake_request)
# Apply the data (if it's returned)
if smart_str(response.content):
try:
field.choices += json.loads(smart_str(response.content))
except ValueError:
raise ValueError('Data returned from request (url={url}, params={params}) could not be deserialized to Python object'.format(
url=url,
params=params
))
field.initial = field_value
except ObjectDoesNotExist:
field.choices = ()