当前位置: 首页>>代码示例>>Python>>正文


Python HttpRequest.GET[key]方法代码示例

本文整理汇总了Python中django.http.request.HttpRequest.GET[key]方法的典型用法代码示例。如果您正苦于以下问题:Python HttpRequest.GET[key]方法的具体用法?Python HttpRequest.GET[key]怎么用?Python HttpRequest.GET[key]使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在django.http.request.HttpRequest的用法示例。


在下文中一共展示了HttpRequest.GET[key]方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: set_choices_via_ajax

# 需要导入模块: from django.http.request import HttpRequest [as 别名]
# 或者: from django.http.request.HttpRequest import GET[key] [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 = ()
开发者ID:CalPolyResDev,项目名称:django-clever-selects,代码行数:74,代码来源:forms.py


注:本文中的django.http.request.HttpRequest.GET[key]方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。