本文整理汇总了Python中django.core.handlers.base.BaseHandler方法的典型用法代码示例。如果您正苦于以下问题:Python base.BaseHandler方法的具体用法?Python base.BaseHandler怎么用?Python base.BaseHandler使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类django.core.handlers.base
的用法示例。
在下文中一共展示了base.BaseHandler方法的7个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_middleware_loads_on_PRE_django_1_10s_old_style_middleware
# 需要导入模块: from django.core.handlers import base [as 别名]
# 或者: from django.core.handlers.base import BaseHandler [as 别名]
def test_middleware_loads_on_PRE_django_1_10s_old_style_middleware(self):
handler = BaseHandler()
handler.load_middleware()
示例2: test_middleware_loads_on_django_1_10s_new_style_middleware
# 需要导入模块: from django.core.handlers import base [as 别名]
# 或者: from django.core.handlers.base import BaseHandler [as 别名]
def test_middleware_loads_on_django_1_10s_new_style_middleware(self):
handler = BaseHandler()
handler.load_middleware()
示例3: make_preview_request
# 需要导入模块: from django.core.handlers import base [as 别名]
# 或者: from django.core.handlers.base import BaseHandler [as 别名]
def make_preview_request(self, original_request=None, preview_mode=None, extra_request_attrs=None):
"""
Simulate a request to this page, by constructing a fake HttpRequest object that is (as far
as possible) representative of a real request to this page's front-end URL, and invoking
serve_preview with that request (and the given preview_mode).
Used for previewing / moderation and any other place where we
want to display a view of this page in the admin interface without going through the regular
page routing logic.
If you pass in a real request object as original_request, additional information (e.g. client IP, cookies)
will be included in the dummy request.
"""
dummy_meta = self._get_dummy_headers(original_request)
request = WSGIRequest(dummy_meta)
# Add a flag to let middleware know that this is a dummy request.
request.is_dummy = True
if extra_request_attrs:
for k, v in extra_request_attrs.items():
setattr(request, k, v)
page = self
# Build a custom django.core.handlers.BaseHandler subclass that invokes serve_preview as
# the eventual view function called at the end of the middleware chain, rather than going
# through the URL resolver
class Handler(BaseHandler):
def _get_response(self, request):
response = page.serve_preview(request, preview_mode)
if hasattr(response, 'render') and callable(response.render):
response = response.render()
return response
# Invoke this custom handler.
handler = Handler()
handler.load_middleware()
return handler.get_response(request)
示例4: get_anonymous_request
# 需要导入模块: from django.core.handlers import base [as 别名]
# 或者: from django.core.handlers.base import BaseHandler [as 别名]
def get_anonymous_request(leonardo_page):
"""returns inicialized request
"""
request_factory = RequestFactory()
request = request_factory.get(
leonardo_page.get_absolute_url(), data={})
request.feincms_page = request.leonardo_page = leonardo_page
request.frontend_editing = False
request.user = AnonymousUser()
if not hasattr(request, '_feincms_extra_context'):
request._feincms_extra_context = {}
request.path = leonardo_page.get_absolute_url()
request.frontend_editing = False
leonardo_page.run_request_processors(request)
request.LEONARDO_CONFIG = ContextConfig(request)
handler = BaseHandler()
handler.load_middleware()
# Apply request middleware
for middleware_method in handler._request_middleware:
try:
middleware_method(request)
except:
pass
# call processors
for fn in reversed(list(leonardo_page.request_processors.values())):
fn(leonardo_page, request)
return request
示例5: __init__
# 需要导入模块: from django.core.handlers import base [as 别名]
# 或者: from django.core.handlers.base import BaseHandler [as 别名]
def __init__(self, *args, **kwargs):
self.handler = BaseHandler()
self.handler.load_middleware()
super().__init__(*args, **kwargs)
示例6: __init__
# 需要导入模块: from django.core.handlers import base [as 别名]
# 或者: from django.core.handlers.base import BaseHandler [as 别名]
def __init__(self, *args, **kwargs):
# create django request resolver
self.handler = BaseHandler()
# prevent recursive includes
old = settings.MIDDLEWARE
name = self.__module__ + '.' + self.__class__.__name__
settings.MIDDLEWARE = [i for i in settings.MIDDLEWARE if i != name]
self.handler.load_middleware()
settings.MIDDLEWARE = old
super(CommonMiddlewareAppendSlashWithoutRedirect, self).__init__(*args, **kwargs)
示例7: build_dummy_request
# 需要导入模块: from django.core.handlers import base [as 别名]
# 或者: from django.core.handlers.base import BaseHandler [as 别名]
def build_dummy_request(newsitem):
"""
Construct a HttpRequest object that is, as far as possible,
representative of ones that would receive this page as a response. Used
for previewing / moderation and any other place where we want to
display a view of this page in the admin interface without going
through the regular page routing logic.
"""
url = newsitem.full_url
if url:
url_info = urlparse(url)
hostname = url_info.hostname
path = url_info.path
port = url_info.port or 80
else:
# Cannot determine a URL to this page - cobble one together based on
# whatever we find in ALLOWED_HOSTS
try:
hostname = settings.ALLOWED_HOSTS[0]
except IndexError:
hostname = 'localhost'
path = '/'
port = 80
request = WSGIRequest({
'REQUEST_METHOD': 'GET',
'PATH_INFO': path,
'SERVER_NAME': hostname,
'SERVER_PORT': port,
'HTTP_HOST': hostname,
'wsgi.input': StringIO(),
})
# Apply middleware to the request - see http://www.mellowmorning.com/2011/04/18/mock-django-request-for-testing/
handler = BaseHandler()
handler.load_middleware()
# call each middleware in turn and throw away any responses that they might return
if hasattr(handler, '_request_middleware'):
for middleware_method in handler._request_middleware:
middleware_method(request)
else:
handler.get_response(request)
return request