admindocs.txt 5.6 KB

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