“Django/docs/2.2.x/ref/class-based-views/mixins-simple”的版本间差异

来自菜鸟教程
Django/docs/2.2.x/ref/class-based-views/mixins-simple
跳转至:导航、​搜索
(autoload)
 
(Page commit)
 
第1行: 第1行:
 +
{{DISPLAYTITLE:简单的 mixins — Django 文档}}
 
<div id="simple-mixins" class="section">
 
<div id="simple-mixins" class="section">
  
= Simple mixins =
+
= 简单混入 =
  
 
<div id="contextmixin" class="section">
 
<div id="contextmixin" class="section">
  
== <code>ContextMixin</code> ==
+
== ContextMixin ==
  
 
<dl>
 
<dl>
<dt>''class'' <code>django.views.generic.base.</code><code>ContextMixin</code></dt>
+
<dt>''<span class="pre">class</span>'' <span class="sig-prename descclassname"><span class="pre">django.views.generic.base.</span></span><span class="sig-name descname"><span class="pre">ContextMixin</span></span></dt>
 
<dd><p>'''属性'''</p>
 
<dd><p>'''属性'''</p>
 
<dl>
 
<dl>
<dt><code>extra_context</code></dt>
+
<dt><span class="sig-name descname"><span class="pre">extra_context</span></span></dt>
<dd><p>A dictionary to include in the context. This is a convenient way of
+
<dd><p>要包含在上下文中的字典。 这是在 [[../base#django.views.generic.base.View|as_view()]] 中指定一些简单上下文的便捷方式。 用法示例:</p>
specifying some simple context in
 
[[../base#django.views.generic.base.View|<code>as_view()</code>]]. Example usage:</p>
 
 
<div class="highlight-default notranslate">
 
<div class="highlight-default notranslate">
  
 
<div class="highlight">
 
<div class="highlight">
  
<pre>from django.views.generic import TemplateView
+
<syntaxhighlight lang="python">from django.views.generic import TemplateView
TemplateView.as_view(extra_context={'title': 'Custom Title'})</pre>
+
TemplateView.as_view(extra_context={'title': 'Custom Title'})</syntaxhighlight>
  
 
</div>
 
</div>
第28行: 第27行:
 
<p>'''方法'''</p>
 
<p>'''方法'''</p>
 
<dl>
 
<dl>
<dt><code>get_context_data</code><span class="sig-paren">(</span>''<span class="o">**</span><span class="n">kwargs</span>''<span class="sig-paren">)</span></dt>
+
<dt><span class="sig-name descname"><span class="pre">get_context_data</span></span><span class="sig-paren">(</span>''<span class="o"><span class="pre">**</span></span><span class="n"><span class="pre">kwargs</span></span>''<span class="sig-paren">)</span></dt>
<dd><p>Returns a dictionary representing the template context. The keyword
+
<dd><p>返回表示模板上下文的字典。 提供的关键字参数将构成返回的上下文。 用法示例:</p>
arguments provided will make up the returned context. Example usage:</p>
 
 
<div class="highlight-default notranslate">
 
<div class="highlight-default notranslate">
  
 
<div class="highlight">
 
<div class="highlight">
  
<pre>def get_context_data(self, **kwargs):
+
<syntaxhighlight lang="python">def get_context_data(self, **kwargs):
 
     context = super().get_context_data(**kwargs)
 
     context = super().get_context_data(**kwargs)
 
     context['number'] = random.randrange(1, 100)
 
     context['number'] = random.randrange(1, 100)
     return context</pre>
+
     return context</syntaxhighlight>
  
 
</div>
 
</div>
  
 
</div>
 
</div>
<p>The template context of all class-based generic views include a
+
<p>所有基于类的通用视图的模板上下文都包含一个 <code>view</code> 变量,该变量指向 <code>View</code> 实例。</p>
<code>view</code> variable that points to the <code>View</code> instance.</p>
 
 
<div class="admonition-use-alters-data-where-appropriate admonition">
 
<div class="admonition-use-alters-data-where-appropriate admonition">
  
<p>Use <code>alters_data</code> where appropriate</p>
+
<p>在适当的地方使用 <code>alters_data</code></p>
<p>Note that having the view instance in the template context may
+
<p>请注意,在模板上下文中拥有视图实例可能会将潜在的危险方法暴露给模板作者。 要防止在模板中调用此类方法,请在这些方法上设置 <code>alters_data=True</code>。 有关更多信息,请阅读有关 [[../../templates/api#alters-data-description|渲染模板上下文]] 的文档。</p>
expose potentially hazardous methods to template authors. To
 
prevent methods like this from being called in the template, set
 
<code>alters_data=True</code> on those methods. For more information, read
 
the documentation on [[../../templates/api#alters-data-description|<span class="std std-ref">rendering a template context</span>]].</p>
 
  
 
</div></dd></dl>
 
</div></dd></dl>
第61行: 第54行:
 
<div id="templateresponsemixin" class="section">
 
<div id="templateresponsemixin" class="section">
  
== <code>TemplateResponseMixin</code> ==
+
== TemplateResponseMixin ==
  
 
<dl>
 
<dl>
<dt>''class'' <code>django.views.generic.base.</code><code>TemplateResponseMixin</code></dt>
+
<dt>''<span class="pre">class</span>'' <span class="sig-prename descclassname"><span class="pre">django.views.generic.base.</span></span><span class="sig-name descname"><span class="pre">TemplateResponseMixin</span></span></dt>
<dd><p>Provides a mechanism to construct a
+
<dd><p>在给定合适的上下文的情况下,提供一种构造 [[../../template-response#django.template.response|TemplateResponse]] 的机制。 要使用的模板是可配置的,并且可以由子类进一步自定义。</p>
[[../../template-response#django.template.response|<code>TemplateResponse</code>]], given
 
suitable context. The template to use is configurable and can be
 
further customized by subclasses.</p>
 
 
<p>'''属性'''</p>
 
<p>'''属性'''</p>
 
<dl>
 
<dl>
<dt><code>template_name</code></dt>
+
<dt><span class="sig-name descname"><span class="pre">template_name</span></span></dt>
<dd><p>The full name of a template to use as defined by a string. Not defining
+
<dd><p>要使用的模板的全名,由字符串定义。 不定义 <code>template_name</code> 将引发 [[../../exceptions#django.core.exceptions|django.core.exceptions.ImproperlyConfigured]] 异常。</p></dd></dl>
a <code>template_name</code> will raise a
 
[[../../exceptions#django.core.exceptions|<code>django.core.exceptions.ImproperlyConfigured</code>]] exception.</p></dd></dl>
 
  
 
<dl>
 
<dl>
<dt><code>template_engine</code></dt>
+
<dt><span class="sig-name descname"><span class="pre">template_engine</span></span></dt>
<dd><p>The [[../../settings#std-setting-TEMPLATES-NAME|<code>NAME</code>]] of a template engine to use for
+
<dd><p>[[#id1|:setting:`名称 `]] 用于加载模板的模板引擎。 <code>template_engine</code> 作为 <code>using</code> 关键字参数传递给 <code>response_class</code>。 默认为 <code>None</code>,它告诉 Django 在所有配置的引擎中搜索模板。</p></dd></dl>
loading the template. <code>template_engine</code> is passed as the <code>using</code>
 
keyword argument to <code>response_class</code>. Default is <code>None</code>, which
 
tells Django to search for the template in all configured engines.</p></dd></dl>
 
  
 
<dl>
 
<dl>
<dt><code>response_class</code></dt>
+
<dt><span class="sig-name descname"><span class="pre">response_class</span></span></dt>
<dd><p>The response class to be returned by <code>render_to_response</code> method.
+
<dd><p><code>render_to_response</code> 方法返回的响应类。 默认值为 [[../../template-response#django.template.response|TemplateResponse]]<code>TemplateResponse</code> 实例的模板和上下文可以稍后更改(例如 在 [[../../../topics/http/middleware#template-response-middleware|模板响应中间件]] 中)。</p>
Default is [[../../template-response#django.template.response|<code>TemplateResponse</code>]]. The template and context
+
<p>如果您需要自定义模板加载或自定义上下文对象实例化,请创建一个 <code>TemplateResponse</code> 子类并将其分配给 <code>response_class</code></p></dd></dl>
of <code>TemplateResponse</code> instances can be altered later (e.g. in
 
[[../../../topics/http/middleware#template-response-middleware|<span class="std std-ref">template response middleware</span>]]).</p>
 
<p>If you need custom template loading or custom context object
 
instantiation, create a <code>TemplateResponse</code> subclass and assign it to
 
<code>response_class</code>.</p></dd></dl>
 
  
 
<dl>
 
<dl>
<dt><code>content_type</code></dt>
+
<dt><span class="sig-name descname"><span class="pre">content_type</span></span></dt>
<dd><p>The content type to use for the response. <code>content_type</code> is passed
+
<dd><p>用于响应的内容类型。 <code>content_type</code> 作为关键字参数传递给 <code>response_class</code>。 默认为 <code>None</code> – 表示 Django 使用 [[#id3|:setting:`DEFAULT_CONTENT_TYPE`]]</p></dd></dl>
as a keyword argument to <code>response_class</code>. Default is <code>None</code> --
 
meaning that Django uses [[../../settings#std-setting-DEFAULT_CONTENT_TYPE|<code>DEFAULT_CONTENT_TYPE</code>]].</p></dd></dl>
 
  
 
<p>'''方法'''</p>
 
<p>'''方法'''</p>
 
<dl>
 
<dl>
<dt><code>render_to_response</code><span class="sig-paren">(</span>''<span class="n">context</span>'', ''<span class="o">**</span><span class="n">response_kwargs</span>''<span class="sig-paren">)</span></dt>
+
<dt><span class="sig-name descname"><span class="pre">render_to_response</span></span><span class="sig-paren">(</span>''<span class="n"><span class="pre">context</span></span>'', ''<span class="o"><span class="pre">**</span></span><span class="n"><span class="pre">response_kwargs</span></span>''<span class="sig-paren">)</span></dt>
<dd><p>Returns a <code>self.response_class</code> instance.</p>
+
<dd><p>返回一个 <code>self.response_class</code> 实例。</p>
<p>If any keyword arguments are provided, they will be passed to the
+
<p>如果提供任何关键字参数,它们将被传递给响应类的构造函数。</p>
constructor of the response class.</p>
+
<p>调用 [[#django.views.generic.base.TemplateResponseMixin.get_template_names|get_template_names()]] 以获取模板名称列表,这些模板名称将被搜索以查找现有模板。</p></dd></dl>
<p>Calls [[#django.views.generic.base.TemplateResponseMixin.get_template_names|<code>get_template_names()</code>]] to obtain the list of template names
 
that will be searched looking for an existent template.</p></dd></dl>
 
  
 
<dl>
 
<dl>
<dt><code>get_template_names</code><span class="sig-paren">(</span><span class="sig-paren">)</span></dt>
+
<dt><span class="sig-name descname"><span class="pre">get_template_names</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span></dt>
<dd><p>Returns a list of template names to search for when rendering the
+
<dd><p>返回要在呈现模板时搜索的模板名称列表。 将使用找到的第一个模板。</p>
template. The first template that is found will be used.</p>
+
<p>如果指定了 [[#django.views.generic.base.TemplateResponseMixin.template_name|template_name]],默认实现将返回一个包含 [[#django.views.generic.base.TemplateResponseMixin.template_name|template_name]](如果指定)的列表。</p></dd></dl>
<p>If [[#django.views.generic.base.TemplateResponseMixin.template_name|<code>template_name</code>]] is specified, the default implementation will
 
return a list containing [[#django.views.generic.base.TemplateResponseMixin.template_name|<code>template_name</code>]] (if it is specified).</p></dd></dl>
 
 
</dd></dl>
 
</dd></dl>
  
第120行: 第94行:
  
 
</div>
 
</div>
 +
<div class="clearer">
  
[[Category:Django 2.2.x 中文文档]]
+
 
 +
 
 +
</div>
 +
 
 +
[[Category:Django 2.2.x 文档]]

2021年10月31日 (日) 04:05的最新版本

简单混入

ContextMixin

class django.views.generic.base.ContextMixin

属性

extra_context

要包含在上下文中的字典。 这是在 as_view() 中指定一些简单上下文的便捷方式。 用法示例:

from django.views.generic import TemplateView
TemplateView.as_view(extra_context={'title': 'Custom Title'})

方法

get_context_data(**kwargs)

返回表示模板上下文的字典。 提供的关键字参数将构成返回的上下文。 用法示例:

def get_context_data(self, **kwargs):
    context = super().get_context_data(**kwargs)
    context['number'] = random.randrange(1, 100)
    return context

所有基于类的通用视图的模板上下文都包含一个 view 变量,该变量指向 View 实例。

在适当的地方使用 alters_data

请注意,在模板上下文中拥有视图实例可能会将潜在的危险方法暴露给模板作者。 要防止在模板中调用此类方法,请在这些方法上设置 alters_data=True。 有关更多信息,请阅读有关 渲染模板上下文 的文档。


TemplateResponseMixin

class django.views.generic.base.TemplateResponseMixin

在给定合适的上下文的情况下,提供一种构造 TemplateResponse 的机制。 要使用的模板是可配置的,并且可以由子类进一步自定义。

属性

template_name

要使用的模板的全名,由字符串定义。 不定义 template_name 将引发 django.core.exceptions.ImproperlyConfigured 异常。

template_engine

:setting:`名称 ` 用于加载模板的模板引擎。 template_engine 作为 using 关键字参数传递给 response_class。 默认为 None,它告诉 Django 在所有配置的引擎中搜索模板。

response_class

render_to_response 方法返回的响应类。 默认值为 TemplateResponseTemplateResponse 实例的模板和上下文可以稍后更改(例如 在 模板响应中间件 中)。

如果您需要自定义模板加载或自定义上下文对象实例化,请创建一个 TemplateResponse 子类并将其分配给 response_class

content_type

用于响应的内容类型。 content_type 作为关键字参数传递给 response_class。 默认为 None – 表示 Django 使用 :setting:`DEFAULT_CONTENT_TYPE`

方法

render_to_response(context, **response_kwargs)

返回一个 self.response_class 实例。

如果提供任何关键字参数,它们将被传递给响应类的构造函数。

调用 get_template_names() 以获取模板名称列表,这些模板名称将被搜索以查找现有模板。

get_template_names()

返回要在呈现模板时搜索的模板名称列表。 将使用找到的第一个模板。

如果指定了 template_name,默认实现将返回一个包含 template_name(如果指定)的列表。