utils.views module¶
- class utils.views.SecureJsonView(**kwargs)[源代码]¶
基类:
SecureView
- ExtraDataType = dict[str, typing.Any] | None¶
- data: dict[str, Any]¶
- http_method_names: list[str] = ['post']¶
- json_response(extra_data: dict[str, Any] | None = None, **kwargs: Any) JsonResponse [源代码]¶
- method_names: list[str] = ['post']¶
- response_class¶
JsonResponse
的别名
- setup(request: HttpRequest, *args: Any, **kwargs: Any) None [源代码]¶
初始化请求参数和返回数据
- class utils.views.SecureTemplateView(**kwargs)[源代码]¶
基类:
SecureView
通用的模板视图类:在SecureView的基础上增加了模板渲染功能
模板渲染: - template_name对应模板的文件名,继承类必须设置这个属性 - get_context_data()用于获取模板所需的context - extra_context作为get_context_data()的补充,在处理请求的过程中可以随时向其中添加内容
- extra_context: dict[str, Any]¶
- permission_denied(user_info: str | None = None) NoReturn [源代码]¶
抛出用户提示,无权访问该页面,必须抛出异常
- 参数:
user_info (str | None, optional) -- 直接呈现给用户的附加信息, defaults to None
- response_class¶
TemplateResponse
的别名
- setup(request: HttpRequest, *args: Any, **kwargs: Any) None [源代码]¶
Initialize attributes shared by all view methods.
- template_name: str¶
- class utils.views.SecureView(**kwargs)[源代码]¶
基类:
View
通用的视图类基类
主要功能:权限检查、方法分发、参数检查
约定: - 以_开头的属性为私有属性,默认只对当前类有效,其它属性对子类有效 - 以_开头的方法为类方法,子类可用,不建议覆盖
权限检查: - 可以根据需要设置perms_required,访问者需要同时具有所有权限才能访问
方法分发 + 参数检查: - 通过`get_method_name`获取`dispatch_prepare`参数名,被`method_names`检查 - 通过`dispatch_prepare`执行该参数的准备过程,并获取处理函数 - 准备函数和处理函数分别对应`class.PrepareType`类型和`class.HandlerType`类型 - 调用处理函数处理最终请求,实现业务逻辑 - 子类可以重载`_dispatch`以强化分发的功能,不建议重载`dispatch`
- HandlerType¶
Callable
[[],HttpResponse
] 的别名
- KWBaseType(fields=None, /, *, total=True, **kwargs)¶
A simple typed namespace. At runtime it is equivalent to a plain dict.
TypedDict creates a dictionary type such that a type checker will expect all instances to have a certain set of keys, where each key is associated with a value of a consistent type. This expectation is not checked at runtime.
Usage:
>>> class Point2D(TypedDict): ... x: int ... y: int ... label: str ... >>> a: Point2D = {'x': 1, 'y': 2, 'label': 'good'} # OK >>> b: Point2D = {'z': 3, 'label': 'bad'} # Fails type check >>> Point2D(x=1, y=2, label='first') == dict(x=1, y=2, label='first') True
The type info can be accessed via the Point2D.__annotations__ dict, and the Point2D.__required_keys__ and Point2D.__optional_keys__ frozensets. TypedDict supports an additional equivalent form:
Point2D = TypedDict('Point2D', {'x': int, 'y': int, 'label': str})
By default, all keys must be present in a TypedDict. It is possible to override this by specifying totality:
class Point2D(TypedDict, total=False): x: int y: int
This means that a Point2D TypedDict can have any of the keys omitted. A type checker is only expected to support a literal False or True as the value of the total argument. True is the default, and makes all items defined in the class body be required.
The Required and NotRequired special forms can also be used to mark individual keys as being required or not required:
class Point2D(TypedDict): x: int # the "x" key must always be present (Required is the default) y: NotRequired[int] # the "y" key can be omitted
See PEP 655 for more details on Required and NotRequired.
- NoReturnPrepareType¶
Callable
[[],Callable
[[],HttpResponse
] |None
] 的别名
- PrepareType¶
Callable
[[],Callable
[[],HttpResponse
]] 的别名
- args: tuple¶
- final default_prepare(method: str, default_name: str | None = None, prepare_needed: bool = True, return_needed: bool = True) Callable[[], HttpResponse] [源代码]¶
默认准备函数,查找并调用特定方法的默认准备函数,不存在时尝试返回处理函数
- 参数:
method (str) -- 处理函数名
default_name (str | None, optional) -- 方法准备函数名,默认是prepare_{方法}, defaults to None
prepare_needed (bool, optional) -- 是否必须执行准备函数, defaults to True
- 抛出:
ImproperlyConfigured -- 必须执行准备函数时,准备函数不存在
ImproperlyConfigured -- 允许且准备函数不存在时,处理函数不存在
- 返回:
处理函数
- 返回类型:
_HandlerFuncType
- dispatch_prepare(method: str) Callable[[], HttpResponse] [源代码]¶
每个方法执行前的准备工作,返回重定向的方法
准备方法的约定以当前类PrepareType为准,PrepareType包含None的可只实现处理方法 SecureView要求必须实现准备方法,子类如果准备方法命名错误未调用则无法提供错误信息 子类建议使用match语句,不存在时可调用`default_prepare`
- error_response(exception: Exception) HttpResponse [源代码]¶
错误处理,异常栈可追溯,生产环境不应产生异常
- get_method_name(request: HttpRequest) str [源代码]¶
- http_forbidden(user_message: str = '') HttpResponse [源代码]¶
- http_method_names: list[str] = ['get', 'post']¶
- kwargs: _KWType¶
- login_required: bool = True¶
- method_names: list[str] = ['get', 'post']¶
- permission_denied(user_info: str | None = None) NoReturn [源代码]¶
抛出用户提示,无权访问该页面,必须抛出异常
- 参数:
user_info (str | None, optional) -- 直接呈现给用户的附加信息, defaults to None
- perms_required: list[str] = []¶
- redirect(to: str, *args: Any, permanent: bool = False, **kwargs: Any)[源代码]¶
重定向,由于类的重定向对象无需提前确定,使用redirect动态加载即可
- final redirect_to_login(request: HttpRequest, login_url: str | None = None) HttpResponseRedirect [源代码]¶
重定向用户至登录页,登录后跳转回当前页面
- 参数:
request (HttpRequest) -- 正在处理的请求
login_url (str, optional) -- 登录页,可包含GET参数,默认使用Django设置, defaults to None
- 返回:
页面重定向
- 返回类型:
HttpResponseRedirect
- request: HttpRequest¶
- response_created(response: HttpResponse) NoReturn [源代码]¶
保存生成的Response,并发送ResponseCreated信号
- 参数:
response (HttpResponse)
- 抛出:
ResponseCreated -- 不包含信息的信号
- setup(request: HttpRequest, *args: Any, **kwargs: Any) None [源代码]¶
Initialize attributes shared by all view methods.