admindocs.txt 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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. You may, to some extent, utilize :mod:`~django.contrib.admindocs` to quickly
  12. document your own code. This has limited usage, however, as the app is
  13. primarily intended for documenting templates, template tags, and filters.
  14. For example, model methods that require arguments are purposefully omitted
  15. from the documentation because they can't be invoked from templates. The app
  16. can still be useful since it doesn't require you to write any extra
  17. documentation (besides docstrings) and is conveniently available from the
  18. :mod:`Django admin <django.contrib.admin>`.
  19. Overview
  20. ========
  21. To activate the :mod:`~django.contrib.admindocs`, you will need to do
  22. the following:
  23. * Add :mod:`django.contrib.admindocs` to your :setting:`INSTALLED_APPS`.
  24. * Add ``(r'^admin/doc/', include('django.contrib.admindocs.urls'))`` to
  25. your ``urlpatterns``. Make sure it's included *before* the
  26. ``r'^admin/'`` entry, so that requests to ``/admin/doc/`` don't get
  27. handled by the latter entry.
  28. * Install the docutils Python module (http://docutils.sf.net/).
  29. * **Optional:** Using the admindocs bookmarklets requires
  30. ``django.contrib.admindocs.middleware.XViewMiddleware`` to be installed.
  31. Once those steps are complete, you can start browsing the documentation by
  32. going to your admin interface and clicking the "Documentation" link in the
  33. upper right of the page.
  34. Documentation helpers
  35. =====================
  36. The following special markup can be used in your docstrings to easily create
  37. hyperlinks to other components:
  38. ================= =======================
  39. Django Component reStructuredText roles
  40. ================= =======================
  41. Models ``:model:`app_label.ModelName```
  42. Views ``:view:`app_label.view_name```
  43. Template tags ``:tag:`tagname```
  44. Template filters ``:filter:`filtername```
  45. Templates ``:template:`path/to/template.html```
  46. ================= =======================
  47. Model reference
  48. ===============
  49. The **models** section of the ``admindocs`` page describes each model in the
  50. system along with all the fields and methods (without any arguments) available
  51. on it. While model properties don't have any arguments, they are not listed.
  52. Relationships to other models appear as hyperlinks. Descriptions are pulled
  53. from ``help_text`` attributes on fields or from docstrings on model methods.
  54. A model with useful documentation might look like this::
  55. class BlogEntry(models.Model):
  56. """
  57. Stores a single blog entry, related to :model:`blog.Blog` and
  58. :model:`auth.User`.
  59. """
  60. slug = models.SlugField(help_text="A short label, generally used in URLs.")
  61. author = models.ForeignKey(User)
  62. blog = models.ForeignKey(Blog)
  63. ...
  64. def publish(self):
  65. """Makes the blog entry live on the site."""
  66. ...
  67. View reference
  68. ==============
  69. Each URL in your site has a separate entry in the ``admindocs`` page, and
  70. clicking on a given URL will show you the corresponding view. Helpful things
  71. you can document in your view function docstrings include:
  72. * A short description of what the view does.
  73. * The **context**, or a list of variables available in the view's template.
  74. * The name of the template or templates that are used for that view.
  75. For example::
  76. from django.shortcuts import render
  77. from myapp.models import MyModel
  78. def my_view(request, slug):
  79. """
  80. Display an individual :model:`myapp.MyModel`.
  81. **Context**
  82. ``mymodel``
  83. An instance of :model:`myapp.MyModel`.
  84. **Template:**
  85. :template:`myapp/my_template.html`
  86. """
  87. context = {'mymodel': MyModel.objects.get(slug=slug)}
  88. return render(request, 'myapp/my_template.html', context)
  89. Template tags and filters reference
  90. ===================================
  91. The **tags** and **filters** ``admindocs`` sections describe all the tags and
  92. filters that come with Django (in fact, the :ref:`built-in tag reference
  93. <ref-templates-builtins-tags>` and :ref:`built-in filter reference
  94. <ref-templates-builtins-filters>` documentation come directly from those
  95. pages). Any tags or filters that you create or are added by a third-party app
  96. will show up in these sections as well.
  97. Template reference
  98. ==================
  99. While ``admindocs`` does not include a place to document templates by
  100. themselves, if you use the ``:template:`path/to/template.html``` syntax in a
  101. docstring the resulting page will verify the path of that template with
  102. Django's :ref:`template loaders <template-loaders>`. This can be a handy way to
  103. check if the specified template exists and to show where on the filesystem that
  104. template is stored.
  105. Included Bookmarklets
  106. =====================
  107. Several useful bookmarklets are available from the ``admindocs`` page:
  108. Documentation for this page
  109. Jumps you from any page to the documentation for the view that generates
  110. that page.
  111. Show object ID
  112. Shows the content-type and unique ID for pages that represent a single
  113. object.
  114. Edit this object
  115. Jumps to the admin page for pages that represent a single object.
  116. Using these bookmarklets requires that you are either logged into the
  117. :mod:`Django admin <django.contrib.admin>` as a
  118. :class:`~django.contrib.auth.models.User` with
  119. :attr:`~django.contrib.auth.models.User.is_staff` set to ``True``, or that the
  120. ``XViewMiddleware`` is installed and you are accessing the site from an IP
  121. address listed in :setting:`INTERNAL_IPS`.