本文整理汇总了Python中django.http.request.HttpRequest方法的典型用法代码示例。如果您正苦于以下问题:Python request.HttpRequest方法的具体用法?Python request.HttpRequest怎么用?Python request.HttpRequest使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类django.http.request
的用法示例。
在下文中一共展示了request.HttpRequest方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: process_view
# 需要导入模块: from django.http import request [as 别名]
# 或者: from django.http.request import HttpRequest [as 别名]
def process_view(
self,
request: HttpRequest,
view_func: Callable,
view_args: Any,
view_kwargs: Any,
) -> None:
"""Add view_func to the profiler info."""
# force the creation of a valid session by saving it.
if (
hasattr(request, "session")
and request.session.session_key is None
and settings.STORE_ANONYMOUS_SESSIONS is True
):
request.session.save()
if hasattr(view_func, "__name__"):
request.profiler.view_func_name = view_func.__name__
else:
request.profiler.view_func_name = view_func.__class__.__name__
示例2: create
# 需要导入模块: from django.http import request [as 别名]
# 或者: from django.http.request import HttpRequest [as 别名]
def create(self, request: HttpRequest, *args, **kwargs) -> Response:
"""
DRF method for creating a Nomination.
Called by the Django Rest Framework in response to the corresponding HTTP request.
"""
for field in request.data:
if field in self.frozen_on_create:
raise ValidationError({field: ['This field cannot be set at creation.']})
user_id = request.data.get("user")
if Nomination.objects.filter(active=True, user__id=user_id).exists():
raise ValidationError({'active': ['There can only be one active nomination.']})
serializer = self.get_serializer(
data=ChainMap(
request.data,
{"active": True}
)
)
serializer.is_valid(raise_exception=True)
self.perform_create(serializer)
headers = self.get_success_headers(serializer.data)
return Response(serializer.data, status=status.HTTP_201_CREATED, headers=headers)
示例3: create
# 需要导入模块: from django.http import request [as 别名]
# 或者: from django.http.request import HttpRequest [as 别名]
def create(self, request: HttpRequest) -> Response:
"""
DRF method for creating a new OffTopicChannelName.
Called by the Django Rest Framework in response to the corresponding HTTP request.
"""
if 'name' in request.query_params:
create_data = {'name': request.query_params['name']}
serializer = OffTopicChannelNameSerializer(data=create_data)
serializer.is_valid(raise_exception=True)
serializer.save()
return Response(create_data, status=HTTP_201_CREATED)
else:
raise ParseError(detail={
'name': ["This query parameter is required."]
})
示例4: list
# 需要导入模块: from django.http import request [as 别名]
# 或者: from django.http.request import HttpRequest [as 别名]
def list(self, request: HttpRequest) -> Response:
"""
DRF method for listing OffTopicChannelName entries.
Called by the Django Rest Framework in response to the corresponding HTTP request.
"""
if 'random_items' in request.query_params:
param = request.query_params['random_items']
try:
random_count = int(param)
except ValueError:
raise ParseError(detail={'random_items': ["Must be a valid integer."]})
if random_count <= 0:
raise ParseError(detail={
'random_items': ["Must be a positive integer."]
})
queryset = self.get_queryset().order_by('?')[:random_count]
serialized = self.serializer_class(queryset, many=True)
return Response(serialized.data)
queryset = self.get_queryset()
serialized = self.serializer_class(queryset, many=True)
return Response(serialized.data)
示例5: get_dummy_request
# 需要导入模块: from django.http import request [as 别名]
# 或者: from django.http.request import HttpRequest [as 别名]
def get_dummy_request(dc, method=None, user=None, system_user=False):
"""
Return dummy request object.
"""
request = HttpRequest()
request.csrf_processing_done = True
request.dc = dc
if method:
request = set_request_method(request, method, copy_request=False)
if system_user:
from api.task.utils import get_system_task_user
request.user = get_system_task_user()
elif user:
request.user = user
return request
示例6: compile_node_actions
# 需要导入模块: from django.http import request [as 别名]
# 或者: from django.http.request import HttpRequest [as 别名]
def compile_node_actions(node, user, request=None, classes=ACTION_CLASSES):
"""Provide :class:`NodeAction` objects for given request.
:param node: The :class:`Node` that the request pertains to.
:param user: The :class:`User` making the request.
:param request: The :class:`HttpRequest` being serviced. It may be used
to obtain information about the OAuth token being used.
:return: An :class:`OrderedDict` mapping applicable actions' display names
to corresponding :class:`NodeAction` instances. The dict is ordered
for consistent display.
"""
actions = (action_class(node, user, request) for action_class in classes)
return OrderedDict(
(action.name, action)
for action in actions
if action.is_actionable() and action.is_permitted()
)
示例7: test_middleware_no_process_request
# 需要导入模块: from django.http import request [as 别名]
# 或者: from django.http.request import HttpRequest [as 别名]
def test_middleware_no_process_request():
middleware = NPlusOneMiddleware()
req, resp = HttpRequest(), HttpResponse()
processed = middleware.process_response(req, resp)
assert processed is resp
示例8: render
# 需要导入模块: from django.http import request [as 别名]
# 或者: from django.http.request import HttpRequest [as 别名]
def render(self):
if not self.template_name:
return None
try:
logger.debug("Template name: %s" % self.template_name)
template = get_template(self.template_name)
except TemplateDoesNotExist:
logger.debug("Template not found: %s" % self.template_name)
return None
# TODO: Avoid using a null HttRequest to context processors
ctx = RequestContext(HttpRequest(), self.ctx)
return template.render(ctx)
示例9: get_context
# 需要导入模块: from django.http import request [as 别名]
# 或者: from django.http.request import HttpRequest [as 别名]
def get_context(self, request: HttpRequest, *args, **kwargs) -> Dict[str, Any]:
context = super().get_context(request, *args, **kwargs)
return {**context, "props": self.get_component_data({"request": request})}
示例10: serve
# 需要导入模块: from django.http import request [as 别名]
# 或者: from django.http.request import HttpRequest [as 别名]
def serve(self, request: HttpRequest, *args, **kwargs) -> HttpResponse:
if self.should_serve_json(request):
json = self.get_component_data({"request": request})
return JsonResponse(json)
return super().serve(request, *args, **kwargs)
示例11: should_serve_json
# 需要导入模块: from django.http import request [as 别名]
# 或者: from django.http.request import HttpRequest [as 别名]
def should_serve_json(request: HttpRequest) -> bool:
return (
request.GET.get("format", None) == "json"
or request.content_type == "application/json"
)
示例12: test_with_host
# 需要导入模块: from django.http import request [as 别名]
# 或者: from django.http.request import HttpRequest [as 别名]
def test_with_host(self):
request = HttpRequest()
request.META = {'HTTP_HOST': 'example.com', 'SERVER_PORT': 80}
self.assertEqual(Site.find_for_request(request), self.site)
示例13: test_with_unknown_host
# 需要导入模块: from django.http import request [as 别名]
# 或者: from django.http.request import HttpRequest [as 别名]
def test_with_unknown_host(self):
request = HttpRequest()
request.META = {'HTTP_HOST': 'unknown.com', 'SERVER_PORT': 80}
self.assertEqual(Site.find_for_request(request), self.default_site)
示例14: test_with_server_name
# 需要导入模块: from django.http import request [as 别名]
# 或者: from django.http.request import HttpRequest [as 别名]
def test_with_server_name(self):
request = HttpRequest()
request.META = {
'SERVER_NAME': 'example.com',
'SERVER_PORT': 80
}
self.assertEqual(Site.find_for_request(request), self.site)
示例15: test_with_x_forwarded_host
# 需要导入模块: from django.http import request [as 别名]
# 或者: from django.http.request import HttpRequest [as 别名]
def test_with_x_forwarded_host(self):
with self.settings(USE_X_FORWARDED_HOST=True):
request = HttpRequest()
request.META = {'HTTP_X_FORWARDED_HOST': 'example.com', 'SERVER_PORT': 80}
self.assertEqual(Site.find_for_request(request), self.site)