“Django/docs/3.0.x/ref/contrib/admin/admindocs”的版本间差异

来自菜鸟教程
Django/docs/3.0.x/ref/contrib/admin/admindocs
跳转至:导航、​搜索
(autoload)
 
(Page commit)
 
第1行: 第1行:
 +
{{DISPLAYTITLE:Django 管理文档生成器 — Django 文档}}
 
<div id="module-django.contrib.admindocs" class="section">
 
<div id="module-django.contrib.admindocs" class="section">
  
 
<span id="the-django-admin-documentation-generator"></span>
 
<span id="the-django-admin-documentation-generator"></span>
= The Django admin documentation generator =
+
= Django 管理文档生成器 =
  
Django's [[#module-django.contrib.admindocs|<code>admindocs</code>]] app pulls documentation from the
+
Django [[#module-django.contrib.admindocs|admindocs]] 应用程序从 [[#id1|:setting:`INSTALLED_APPS`]] 中的任何应用程序的模型、视图、模板标签和模板过滤器的文档字符串中提取文档,并从 [[index.html#module-django.contrib.admin|中提供该文档]Django 管理员]]
docstrings of models, views, template tags, and template filters for any app in
 
[[../../../settings#std-setting-INSTALLED_APPS|<code>INSTALLED_APPS</code>]] and makes that documentation available from the
 
[[../index#module-django.contrib|<code>Django admin</code>]].
 
  
 
<div id="overview" class="section">
 
<div id="overview" class="section">
  
== 概况 ==
+
== 概览 ==
  
To activate the [[#module-django.contrib.admindocs|<code>admindocs</code>]], you will need to do
+
要激活 [[#module-django.contrib.admindocs|admindocs]],您需要执行以下操作:
the following:
 
  
* Add [[#module-django.contrib.admindocs|<code>django.contrib.admindocs</code>]] to your [[../../../settings#std-setting-INSTALLED_APPS|<code>INSTALLED_APPS</code>]].
+
* [[#module-django.contrib.admindocs|django.contrib.admindocs]] 添加到您的 [[#id3|:setting:`INSTALLED_APPS`]]
* Add <code>path('admin/doc/', include('django.contrib.admindocs.urls'))</code> to your <code>urlpatterns</code>. Make sure it's included ''before'' the <code>'admin/'</code> entry, so that requests to <code>/admin/doc/</code> don't get handled by the latter entry.
+
* <code>path('admin/doc/', include('django.contrib.admindocs.urls'))</code> 添加到您的 <code>urlpatterns</code>。 确保在 ''<code>'admin/'</code> 条目之前包含'' ,以便对 <code>/admin/doc/</code> 的请求不会被后一个条目处理。
* Install the docutils Python module (https://docutils.sourceforge.io/).
+
* 安装 docutils Python 模块 (https://docutils.sourceforge.io/)
* '''Optional:''' Using the admindocs bookmarklets requires <code>django.contrib.admindocs.middleware.XViewMiddleware</code> to be installed.
+
* '''可选:''' 使用 admindocs 书签需要安装 <code>django.contrib.admindocs.middleware.XViewMiddleware</code>
  
Once those steps are complete, you can start browsing the documentation by
+
完成这些步骤后,您可以通过转到管理界面并单击页面右上角的“文档”链接来开始浏览文档。
going to your admin interface and clicking the &quot;Documentation&quot; link in the
 
upper right of the page.
 
  
  
第29行: 第24行:
 
<div id="documentation-helpers" class="section">
 
<div id="documentation-helpers" class="section">
  
== Documentation helpers ==
+
== 文档助手 ==
  
The following special markup can be used in your docstrings to easily create
+
可以在您的文档字符串中使用以下特殊标记轻松创建指向其他组件的超链接:
hyperlinks to other components:
 
  
 
{|
 
{|
!width="31%"| Django Component
+
!width="31%"| Django 组件
!width="69%"| reStructuredText roles
+
!width="69%"| 重构文本角色
 
|-
 
|-
| 模型
+
| 型号
 
| <code>:model:`app_label.ModelName`</code>
 
| <code>:model:`app_label.ModelName`</code>
 
|-
 
|-
| 视图
+
| 观看次数
 
| <code>:view:`app_label.view_name`</code>
 
| <code>:view:`app_label.view_name`</code>
 
|-
 
|-
第58行: 第52行:
 
<div id="model-reference" class="section">
 
<div id="model-reference" class="section">
  
== Model reference ==
+
== 型号参考 ==
  
The '''models''' section of the <code>admindocs</code> page describes each model in the
+
<code>admindocs</code> 页面的 '''models''' 部分描述了系统中的每个模型及其上可用的所有字段、属性和方法。 与其他模型的关系显示为超链接。 描述是从字段的 <code>help_text</code> 属性或模型方法的文档字符串中提取的。
system along with all the fields, properties, and methods available on it.
 
Relationships to other models appear as hyperlinks. Descriptions are pulled
 
from <code>help_text</code> attributes on fields or from docstrings on model methods.
 
  
 
<div class="versionchanged">
 
<div class="versionchanged">
  
Older versions don't display model properties.
+
<span class="versionmodified changed"> 2.2 版更改: </span> 旧版本不显示模型属性。
  
  
 
</div>
 
</div>
A model with useful documentation might look like this:
+
带有有用文档的模型可能如下所示:
  
 
<div class="highlight-default notranslate">
 
<div class="highlight-default notranslate">
第77行: 第68行:
 
<div class="highlight">
 
<div class="highlight">
  
<pre>class BlogEntry(models.Model):
+
<syntaxhighlight lang="python">class BlogEntry(models.Model):
     &quot;&quot;&quot;
+
     """
 
     Stores a single blog entry, related to :model:`blog.Blog` and
 
     Stores a single blog entry, related to :model:`blog.Blog` and
 
     :model:`auth.User`.
 
     :model:`auth.User`.
     &quot;&quot;&quot;
+
     """
     slug = models.SlugField(help_text=&quot;A short label, generally used in URLs.&quot;)
+
     slug = models.SlugField(help_text="A short label, generally used in URLs.")
 
     author = models.ForeignKey(
 
     author = models.ForeignKey(
 
         User,
 
         User,
第92行: 第83行:
  
 
     def publish(self):
 
     def publish(self):
         &quot;&quot;&quot;Makes the blog entry live on the site.&quot;&quot;&quot;
+
         """Makes the blog entry live on the site."""
         ...</pre>
+
         ...</syntaxhighlight>
  
 
</div>
 
</div>
第102行: 第93行:
 
<div id="view-reference" class="section">
 
<div id="view-reference" class="section">
  
== View reference ==
+
== 查看参考 ==
  
Each URL in your site has a separate entry in the <code>admindocs</code> page, and
+
您站点中的每个 URL <code>admindocs</code> 页面中都有一个单独的条目,单击给定的 URL 将显示相应的视图。 您可以在视图函数文档字符串中记录的有用内容包括:
clicking on a given URL will show you the corresponding view. Helpful things
 
you can document in your view function docstrings include:
 
  
* A short description of what the view does.
+
* 视图功能的简短描述。
* The '''context''', or a list of variables available in the view's template.
+
* '''context''',或视图模板中可用的变量列表。
* The name of the template or templates that are used for that view.
+
* 用于该视图的模板或模板的名称。
  
例子:
+
例如:
  
 
<div class="highlight-default notranslate">
 
<div class="highlight-default notranslate">
第118行: 第107行:
 
<div class="highlight">
 
<div class="highlight">
  
<pre>from django.shortcuts import render
+
<syntaxhighlight lang="python">from django.shortcuts import render
  
 
from myapp.models import MyModel
 
from myapp.models import MyModel
  
 
def my_view(request, slug):
 
def my_view(request, slug):
     &quot;&quot;&quot;
+
     """
 
     Display an individual :model:`myapp.MyModel`.
 
     Display an individual :model:`myapp.MyModel`.
  
第134行: 第123行:
  
 
     :template:`myapp/my_template.html`
 
     :template:`myapp/my_template.html`
     &quot;&quot;&quot;
+
     """
 
     context = {'mymodel': MyModel.objects.get(slug=slug)}
 
     context = {'mymodel': MyModel.objects.get(slug=slug)}
     return render(request, 'myapp/my_template.html', context)</pre>
+
     return render(request, 'myapp/my_template.html', context)</syntaxhighlight>
  
 
</div>
 
</div>
第145行: 第134行:
 
<div id="template-tags-and-filters-reference" class="section">
 
<div id="template-tags-and-filters-reference" class="section">
  
== Template tags and filters reference ==
+
== 模板标签和过滤器参考 ==
  
The '''tags''' and '''filters''' <code>admindocs</code> sections describe all the tags and
+
'''tags''' '''filters''' <code>admindocs</code> 部分描述了 Django 附带的所有标签和过滤器(实际上, [[../../../templates/builtins#ref-templates-builtins-tags|内置标签参考]] [[../../../templates/builtins#ref-templates-builtins-filters|内置过滤器参考]] 文档直接来自这些页面)。 您创建或由第三方应用程序添加的任何标签或过滤器也将显示在这些部分中。
filters that come with Django (in fact, the [[../../../templates/builtins#ref-templates-builtins-tags|<span class="std std-ref">built-in tag reference</span>]] and [[../../../templates/builtins#ref-templates-builtins-filters|<span class="std std-ref">built-in filter reference</span>]] documentation come directly from those
 
pages). Any tags or filters that you create or are added by a third-party app
 
will show up in these sections as well.
 
  
  
第156行: 第142行:
 
<div id="template-reference" class="section">
 
<div id="template-reference" class="section">
  
== Template reference ==
+
== 模板参考 ==
  
While <code>admindocs</code> does not include a place to document templates by
+
虽然 <code>admindocs</code> 不包含单独记录模板的位置,但如果您在文档字符串中使用 <code>:template:`path/to/template.html`</code> 语法,生成的页面将使用 Django 的 模板加载器验证该模板的路径[ X216X]。 这是检查指定模板是否存在以及显示模板存储在文件系统上的位置的一种方便的方法。
themselves, if you use the <code>:template:`path/to/template.html`</code> syntax in a
 
docstring the resulting page will verify the path of that template with
 
Django's [[../../../templates/api#template-loaders|<span class="std std-ref">template loaders</span>]]. This can be a handy way to
 
check if the specified template exists and to show where on the filesystem that
 
template is stored.
 
  
  
第170行: 第151行:
  
 
<span id="admindocs-bookmarklets"></span>
 
<span id="admindocs-bookmarklets"></span>
== Included Bookmarklets ==
+
== 包括书签 ==
  
One bookmarklet is available from the <code>admindocs</code> page:
+
<code>admindocs</code> 页面提供了一个书签:
  
; 关于本页面的文档
+
; 此页面的文档
: 从任何页面跳转到生成该页面的 view 文档。
+
: 将您从任何页面跳转到生成该页面的视图的文档。
  
Using this bookmarklet requires that <code>XViewMiddleware</code> is installed and that
+
使用此书签需要安装 <code>XViewMiddleware</code> 并且您以 [[../../auth#django.contrib.auth.models|User]] 的身份登录 [[../index#module-django.contrib|Django admin]],并将 [[../../auth#django.contrib.auth.models.User|is_staff]] 设置为 <code>True</code>
you are logged into the [[../index#module-django.contrib|<code>Django admin</code>]] as a
 
[[../../auth#django.contrib.auth.models|<code>User</code>]] with
 
[[../../auth#django.contrib.auth.models.User|<code>is_staff</code>]] set to <code>True</code>.
 
  
  
第186行: 第164行:
  
 
</div>
 
</div>
 +
<div class="clearer">
  
[[Category:Django 3.0.x 中文文档]]
+
 
 +
 
 +
</div>
 +
 
 +
[[Category:Django 3.0.x 文档]]

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

Django 管理文档生成器

Django 的 admindocs 应用程序从 :setting:`INSTALLED_APPS` 中的任何应用程序的模型、视图、模板标签和模板过滤器的文档字符串中提取文档,并从 中提供该文档]Django 管理员

概览

要激活 admindocs,您需要执行以下操作:

  • django.contrib.admindocs 添加到您的 :setting:`INSTALLED_APPS`
  • path('admin/doc/', include('django.contrib.admindocs.urls')) 添加到您的 urlpatterns。 确保在 'admin/' 条目之前包含 ,以便对 /admin/doc/ 的请求不会被后一个条目处理。
  • 安装 docutils Python 模块 (https://docutils.sourceforge.io/)。
  • 可选: 使用 admindocs 书签需要安装 django.contrib.admindocs.middleware.XViewMiddleware

完成这些步骤后,您可以通过转到管理界面并单击页面右上角的“文档”链接来开始浏览文档。


文档助手

可以在您的文档字符串中使用以下特殊标记轻松创建指向其他组件的超链接:

Django 组件 重构文本角色
型号 :model:`app_label.ModelName`
观看次数 :view:`app_label.view_name`
模板标签 :tag:`tagname`
模板过滤器 :filter:`filtername`
模板 :template:`path/to/template.html`


型号参考

admindocs 页面的 models 部分描述了系统中的每个模型及其上可用的所有字段、属性和方法。 与其他模型的关系显示为超链接。 描述是从字段的 help_text 属性或模型方法的文档字符串中提取的。

2.2 版更改: 旧版本不显示模型属性。


带有有用文档的模型可能如下所示:

class BlogEntry(models.Model):
    """
    Stores a single blog entry, related to :model:`blog.Blog` and
    :model:`auth.User`.
    """
    slug = models.SlugField(help_text="A short label, generally used in URLs.")
    author = models.ForeignKey(
        User,
        models.SET_NULL,
        blank=True, null=True,
    )
    blog = models.ForeignKey(Blog, models.CASCADE)
    ...

    def publish(self):
        """Makes the blog entry live on the site."""
        ...

查看参考

您站点中的每个 URL 在 admindocs 页面中都有一个单独的条目,单击给定的 URL 将显示相应的视图。 您可以在视图函数文档字符串中记录的有用内容包括:

  • 视图功能的简短描述。
  • context,或视图模板中可用的变量列表。
  • 用于该视图的模板或模板的名称。

例如:

from django.shortcuts import render

from myapp.models import MyModel

def my_view(request, slug):
    """
    Display an individual :model:`myapp.MyModel`.

    **Context**

    ``mymodel``
        An instance of :model:`myapp.MyModel`.

    **Template:**

    :template:`myapp/my_template.html`
    """
    context = {'mymodel': MyModel.objects.get(slug=slug)}
    return render(request, 'myapp/my_template.html', context)

模板标签和过滤器参考

tagsfilters admindocs 部分描述了 Django 附带的所有标签和过滤器(实际上, 内置标签参考内置过滤器参考 文档直接来自这些页面)。 您创建或由第三方应用程序添加的任何标签或过滤器也将显示在这些部分中。


模板参考

虽然 admindocs 不包含单独记录模板的位置,但如果您在文档字符串中使用 :template:`path/to/template.html` 语法,生成的页面将使用 Django 的 模板加载器验证该模板的路径[ X216X]。 这是检查指定模板是否存在以及显示模板存储在文件系统上的位置的一种方便的方法。


包括书签

admindocs 页面提供了一个书签:

此页面的文档
将您从任何页面跳转到生成该页面的视图的文档。

使用此书签需要安装 XViewMiddleware 并且您以 User 的身份登录 Django admin,并将 is_staff 设置为 True