SingleObjectMixin
¶django.views.generic.detail.
SingleObjectMixin
¶提供用于查找与当前HTTP请求关联的对象的机制。
Methods and Attributes
queryset
¶用于表示对象的QuerySet
。 如果提供,queryset
的值将取代为model
提供的值。
Warning
queryset
是一个具有可变值的类属性,因此在直接使用它时必须小心。 在使用它之前,要么调用它的all()
方法,要么使用get_queryset()
检索它,它将负责幕后的克隆。
slug_field
¶包含slug的模型上的字段名称。 By default,
slug_field
is 'slug'
.
slug_url_kwarg
¶包含slug的URLConf关键字参数的名称。 默认情况下,slug_url_kwarg
是'slug'
。
pk_url_kwarg
¶包含主键的URLConf关键字参数的名称。
默认情况下,pk_url_kwarg
是'pk'
。
context_object_name
¶指定要在上下文中使用的变量的名称。
query_pk_and_slug
¶如果True
,则导致get_object()
使用主键和slug执行查找。 默认为False
。
此属性可以帮助缓解不安全的直接对象引用攻击。 当应用程序允许通过顺序主键访问单个对象时,攻击者可以强制猜测所有URL;从而获得应用程序中所有对象的列表。 如果应阻止有权访问单个对象的用户获取此列表,则将query_pk_and_slug
设置为True
将有助于防止猜测URL,因为每个URL都需要两个正确的,非顺序参数。 简单地使用一个独特的slug可能会起到同样的作用,但这种方案允许你拥有非独特的slu ..
get_object
(queryset=None)¶Returns the single object that this view will display. 如果提供了queryset
,则该查询集将用作对象的源;否则,将使用get_queryset()
。 get_object()
在视图的参数中查找pk_url_kwarg
参数;如果找到此参数,则此方法使用该值执行基于主键的查找。 如果未找到此参数,则会查找slug_url_kwarg
参数,并使用slug_field
执行段塞查找。
当query_pk_and_slug
True
时,get_object()
将使用主键和slug执行查找。
get_queryset
()¶Returns the queryset that will be used to retrieve the object that
this view will display. By default, get_queryset()
returns the
value of the queryset
attribute if it is set, otherwise
it constructs a QuerySet
by calling
the all()
method on the model
attribute’s default manager.
get_context_object_name
(obj)¶Return the context variable name that will be used to contain the
data that this view is manipulating. If context_object_name
is
not set, the context name will be constructed from the model_name
of the model that the queryset is composed from. For example, the model
Article
would have context object named 'article'
.
get_context_data
(**kwargs)¶Returns context data for displaying the object.
The base implementation of this method requires that the self.object
attribute be set by the view (even if None
). Be sure to do this if
you are using this mixin without one of the built-in views that does so.
It returns a dictionary with these contents:
object
: The object that this view is displaying
(self.object
).context_object_name
: self.object
will also be stored under
the name returned by get_context_object_name()
, which defaults
to the lowercased version of the model name.Context variables override values from template context processors
Any variables from get_context_data()
take precedence over
context variables from context processors. For example, if your view
sets the model
attribute to
User
, the default context
object name of user
would override the user
variable from
the django.contrib.auth.context_processors.auth()
context
processor. Use get_context_object_name()
to avoid a clash.
get_slug_field
()¶Returns the name of a slug field to be used to look up by slug. By
default this simply returns the value of slug_field
.
SingleObjectTemplateResponseMixin
¶django.views.generic.detail.
SingleObjectTemplateResponseMixin
¶一个mixin类,它为对单个对象实例进行操作的视图执行基于模板的响应呈现。 Requires that the view it is
mixed with provides self.object
, the object instance that the view is
operating on. self.object
will usually be, but is not required to be,
an instance of a Django model. It may be None
if the view is in the
process of constructing a new instance.
Extends
Methods and Attributes
template_name_field
¶The field on the current object instance that can be used to determine
the name of a candidate template. If either template_name_field
itself or the value of the template_name_field
on the current
object instance is None
, the object will not be used for a
candidate template name.
template_name_suffix
¶The suffix to append to the auto-generated candidate template name.
Default suffix is _detail
.
get_template_names
()¶Returns a list of candidate template names. Returns the following list:
template_name
on the view (if provided)template_name_field
field on the
object instance that the view is operating upon (if available)<app_label>/<model_name><template_name_suffix>.html
Jan 17, 2018