2
0

admindocs.txt 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. ========================================
  2. The Django admin documentation generator
  3. ========================================
  4. .. module:: django.contrib.admindocs
  5. :synopsis: Django's admin documentation generator.
  6. .. currentmodule:: django.contrib.admindocs
  7. Django's :mod:`~django.contrib.admindocs` app pulls documentation from the
  8. docstrings of models, views, template tags, and template filters for any app in
  9. :setting:`INSTALLED_APPS` and makes that documentation available from the
  10. :mod:`Django admin <django.contrib.admin>`.
  11. Overview
  12. ========
  13. To activate the :mod:`~django.contrib.admindocs`, you will need to do
  14. the following:
  15. * Add :mod:`django.contrib.admindocs` to your :setting:`INSTALLED_APPS`.
  16. * Add ``path('admin/doc/', include('django.contrib.admindocs.urls'))`` to
  17. your ``urlpatterns``. Make sure it's included *before* the
  18. ``'admin/'`` entry, so that requests to ``/admin/doc/`` don't get
  19. handled by the latter entry.
  20. * Install the :pypi:`docutils` 0.19+ package.
  21. * **Optional:** Using the admindocs bookmarklets requires
  22. ``django.contrib.admindocs.middleware.XViewMiddleware`` to be installed.
  23. Once those steps are complete, you can start browsing the documentation by
  24. going to your admin interface and clicking the "Documentation" link in the
  25. upper right of the page.
  26. .. _admindocs-helpers:
  27. Documentation helpers
  28. =====================
  29. The following special markup can be used in your docstrings to easily create
  30. hyperlinks to other components:
  31. ================= =======================
  32. Django Component reStructuredText roles
  33. ================= =======================
  34. Models ``:model:`app_label.ModelName```
  35. Views ``:view:`app_label.view_name```
  36. Template tags ``:tag:`tagname```
  37. Template filters ``:filter:`filtername```
  38. Templates ``:template:`path/to/template.html```
  39. ================= =======================
  40. Each of these support custom link text with the format
  41. ``:role:`link text <link>```. For example, ``:tag:`block <built_in-block>```.
  42. .. versionchanged:: 5.2
  43. Support for custom link text was added.
  44. .. _admindocs-model-reference:
  45. Model reference
  46. ===============
  47. The **models** section of the ``admindocs`` page describes each model that the
  48. user has access to along with all the fields, properties, and methods available
  49. on it. Relationships to other models appear as hyperlinks. Descriptions are
  50. pulled from ``help_text`` attributes on fields or from docstrings on model
  51. methods.
  52. A model with useful documentation might look like this::
  53. class BlogEntry(models.Model):
  54. """
  55. Stores a single blog entry, related to :model:`blog.Blog` and
  56. :model:`auth.User`.
  57. """
  58. slug = models.SlugField(help_text="A short label, generally used in URLs.")
  59. author = models.ForeignKey(
  60. User,
  61. models.SET_NULL,
  62. blank=True,
  63. null=True,
  64. )
  65. blog = models.ForeignKey(Blog, models.CASCADE)
  66. ...
  67. def publish(self):
  68. """Makes the blog entry live on the site."""
  69. ...
  70. .. versionchanged:: 5.2
  71. Access was restricted to only allow users with model view or change
  72. permissions.
  73. View reference
  74. ==============
  75. Each URL in your site has a separate entry in the ``admindocs`` page, and
  76. clicking on a given URL will show you the corresponding view. Helpful things
  77. you can document in your view function docstrings include:
  78. * A short description of what the view does.
  79. * The **context**, or a list of variables available in the view's template.
  80. * The name of the template or templates that are used for that view.
  81. For example::
  82. from django.shortcuts import render
  83. from myapp.models import MyModel
  84. def my_view(request, slug):
  85. """
  86. Display an individual :model:`myapp.MyModel`.
  87. **Context**
  88. ``mymodel``
  89. An instance of :model:`myapp.MyModel`.
  90. **Template:**
  91. :template:`myapp/my_template.html`
  92. """
  93. context = {"mymodel": MyModel.objects.get(slug=slug)}
  94. return render(request, "myapp/my_template.html", context)
  95. Template tags and filters reference
  96. ===================================
  97. The **tags** and **filters** ``admindocs`` sections describe all the tags and
  98. filters that come with Django (in fact, the :ref:`built-in tag reference
  99. <ref-templates-builtins-tags>` and :ref:`built-in filter reference
  100. <ref-templates-builtins-filters>` documentation come directly from those
  101. pages). Any tags or filters that you create or are added by a third-party app
  102. will show up in these sections as well.
  103. Template reference
  104. ==================
  105. While ``admindocs`` does not include a place to document templates by
  106. themselves, if you use the ``:template:`path/to/template.html``` syntax in a
  107. docstring the resulting page will verify the path of that template with
  108. Django's :ref:`template loaders <template-loaders>`. This can be a handy way to
  109. check if the specified template exists and to show where on the filesystem that
  110. template is stored.
  111. .. _admindocs-bookmarklets:
  112. Included Bookmarklets
  113. =====================
  114. One bookmarklet is available from the ``admindocs`` page:
  115. Documentation for this page
  116. Jumps you from any page to the documentation for the view that generates
  117. that page.
  118. Using this bookmarklet requires that ``XViewMiddleware`` is installed and that
  119. you are logged into the :mod:`Django admin <django.contrib.admin>` as a
  120. :class:`~django.contrib.auth.models.User` with
  121. :attr:`~django.contrib.auth.models.User.is_staff` set to ``True``.