本文整理汇总了Python中django.views.View方法的典型用法代码示例。如果您正苦于以下问题:Python views.View方法的具体用法?Python views.View怎么用?Python views.View使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类django.views
的用法示例。
在下文中一共展示了views.View方法的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: request_mapping
# 需要导入模块: from django import views [as 别名]
# 或者: from django.views import View [as 别名]
def request_mapping(value: str, method: str = 'get', path_type: str = 'path'):
"""
:param value: The path mapping URIs (e.g. "/myPath.do")
:param method: The HTTP request methods to map to, narrowing the primary mapping:
get, post, head, options, put, patch, delete, trace
:param path_type: path or re_path
"""
# todo: type annotation error
def get_func(o: type(View), v: str):
setattr(o, 'request_mapping', RequestMapping(v, method, path_type))
if inspect.isclass(o):
if not value.startswith('/'):
logger.warning("values should startswith / ")
o.as_django_request_mapping_view = as_django_request_mapping_view
o.django_request_mapping_dispatch = django_request_mapping_dispatch
return o
return partial(get_func, v=value)
示例2: dispatch
# 需要导入模块: from django import views [as 别名]
# 或者: from django.views import View [as 别名]
def dispatch(self, request, *args, **kwargs):
# type: (HttpRequest, object, object) -> HttpResponse
"""Inspect the HTTP method and delegate to the view method.
This is the default implementation of the
:py:class:`django.views.View` method, which will inspect the
HTTP method in the input request and delegate it to the
corresponding method in the view. The only allowed method on
this view is ``post``.
:param request: The input request sent to the view
:type request: django.http.HttpRequest
:return: The response from the view
:rtype: django.http.HttpResponse
:raises: :py:class:`django.http.HttpResponseNotAllowed` if the
method is invoked for other than HTTP POST request.
:py:class:`django.http.HttpResponseBadRequest` if the
request verification fails.
:py:class:`django.http.HttpResponseServerError` for any
internal exception.
"""
return super(SkillAdapter, self).dispatch(request)
示例3: get
# 需要导入模块: from django import views [as 别名]
# 或者: from django.views import View [as 别名]
def get(self, request, *args, **kwargs):
return JsonResponse(json.loads(serialize('canvas', self.get_queryset())))
# TODO It should be okay to remove this.
# class StartingCanvas(View):
# def get_queryset(self):
# return Canvas.objects.filter(is_starting_page=True)
# def get(self, request, *args, **kwargs):
# return JsonResponse(json.loads(serialize('startingcanvas', self.get_queryset(), is_list=True)), safe=False)
示例4: get
# 需要导入模块: from django import views [as 别名]
# 或者: from django.views import View [as 别名]
def get(self, request):
kw = request.GET.get('kw')
results = dict()
if kw:
results['user'] = query_user(kw)
results['problem'] = query_problem(kw, all=is_admin_or_root(request.user))
results['tag'] = query_tag(kw)
results['blog'] = query_blog(kw)
results['contest'] = query_contest(kw)
return JsonResponse(dict(results=results, action={
"url": reverse('search') + '?q=%s' % kw,
"text": "View all results"
}))
return JsonResponse(dict(results=results))
示例5: wrap_context
# 需要导入模块: from django import views [as 别名]
# 或者: from django.views import View [as 别名]
def wrap_context(context):
""" Wraps a django context dict into list of tuples
:param context: django context dict
:return: (list)
"""
def wrap(key, val):
""" Wraps a context element in a tuple.
Result tuple contains:
* name - name of context element
* value - value of context element
* class - repr class name of a value
* ext - flag of type group for extension
* count - length of value if it's possible or makes sense
:param key: name of context element.
:param val: value of context element.
:return: (tuple)
"""
cls, ext, count = val.__class__.__name__, Ext.CLASS, 0
if isinstance(val, TYPE_BUILTINS):
if not hasattr(val, '__len__') and not hasattr(val, '__iter__'):
ext = Ext.SIMPLE
else:
ext, count = Ext.ITERABLE, len(val)
elif isinstance(val, TYPE_DJANGO):
ext = Ext.DJANGO
if isinstance(val, QuerySet):
count = len(val)
cls = 'QuerySet: %s' % val.model.__name__
elif isinstance(val, Model):
cls, count = 'Model: %s' % val.__class__.__name__, 1
elif isinstance(val, Form):
cls, count = 'Form: %s' % val.__class__.__name__, 1
else:
if hasattr(val, '__name__'):
cls = 'Class: %s' % val.__name__
else:
cls = 'Instance: %s' % val.__class__.__name__
# for ugly representation other classes and instances
val = val if len(str(val)) < 50 else '%s...' % str(val)[:50]
return key, val, cls, ext, count
items = [wrap(k, v) for k, v in context.items() if not isinstance(v, View)]
return sorted(items, key=lambda item: item[3])