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