2010-11-07 17:21:55 +08:00
|
|
|
========================================
|
|
|
|
The Django admin documentation generator
|
|
|
|
========================================
|
|
|
|
|
|
|
|
.. module:: django.contrib.admindocs
|
|
|
|
:synopsis: Django's admin documentation generator.
|
|
|
|
|
|
|
|
.. currentmodule:: django.contrib.admindocs
|
|
|
|
|
|
|
|
Django's :mod:`~django.contrib.admindocs` app pulls documentation from the
|
|
|
|
docstrings of models, views, template tags, and template filters for any app in
|
|
|
|
:setting:`INSTALLED_APPS` and makes that documentation available from the
|
|
|
|
:mod:`Django admin <django.contrib.admin>`.
|
|
|
|
|
2014-10-31 03:45:01 +08:00
|
|
|
You may, to some extent, utilize :mod:`~django.contrib.admindocs` to quickly
|
|
|
|
document your own code. This has limited usage, however, as the app is
|
|
|
|
primarily intended for documenting templates, template tags, and filters.
|
|
|
|
For example, model methods that require arguments are purposefully omitted
|
|
|
|
from the documentation because they can't be invoked from templates. The app
|
|
|
|
can still be useful since it doesn't require you to write any extra
|
|
|
|
documentation (besides docstrings) and is conveniently available from the
|
|
|
|
:mod:`Django admin <django.contrib.admin>`.
|
2010-11-07 17:21:55 +08:00
|
|
|
|
|
|
|
Overview
|
|
|
|
========
|
|
|
|
|
|
|
|
To activate the :mod:`~django.contrib.admindocs`, you will need to do
|
|
|
|
the following:
|
|
|
|
|
2011-10-14 08:12:01 +08:00
|
|
|
* Add :mod:`django.contrib.admindocs` to your :setting:`INSTALLED_APPS`.
|
|
|
|
* Add ``(r'^admin/doc/', include('django.contrib.admindocs.urls'))`` to
|
2013-01-01 21:12:42 +08:00
|
|
|
your ``urlpatterns``. Make sure it's included *before* the
|
2011-10-14 08:12:01 +08:00
|
|
|
``r'^admin/'`` entry, so that requests to ``/admin/doc/`` don't get
|
|
|
|
handled by the latter entry.
|
|
|
|
* Install the docutils Python module (http://docutils.sf.net/).
|
2013-05-20 22:17:49 +08:00
|
|
|
* **Optional:** Using the admindocs bookmarklets requires
|
|
|
|
``django.contrib.admindocs.middleware.XViewMiddleware`` to be installed.
|
2010-11-07 17:21:55 +08:00
|
|
|
|
|
|
|
Once those steps are complete, you can start browsing the documentation by
|
|
|
|
going to your admin interface and clicking the "Documentation" link in the
|
|
|
|
upper right of the page.
|
|
|
|
|
|
|
|
Documentation helpers
|
|
|
|
=====================
|
|
|
|
|
|
|
|
The following special markup can be used in your docstrings to easily create
|
|
|
|
hyperlinks to other components:
|
|
|
|
|
|
|
|
================= =======================
|
|
|
|
Django Component reStructuredText roles
|
|
|
|
================= =======================
|
2013-12-28 16:53:02 +08:00
|
|
|
Models ``:model:`app_label.ModelName```
|
|
|
|
Views ``:view:`app_label.view_name```
|
2010-11-07 17:21:55 +08:00
|
|
|
Template tags ``:tag:`tagname```
|
|
|
|
Template filters ``:filter:`filtername```
|
|
|
|
Templates ``:template:`path/to/template.html```
|
|
|
|
================= =======================
|
|
|
|
|
|
|
|
Model reference
|
|
|
|
===============
|
|
|
|
|
|
|
|
The **models** section of the ``admindocs`` page describes each model in the
|
2014-10-31 03:45:01 +08:00
|
|
|
system along with all the fields and methods available on it. Relationships
|
|
|
|
to other models appear as hyperlinks. Descriptions are pulled from ``help_text``
|
|
|
|
attributes on fields or from docstrings on model methods.
|
|
|
|
|
|
|
|
.. versionchanged:: 1.9
|
|
|
|
|
|
|
|
The **models** section of the ``admindocs`` now describes methods that take
|
|
|
|
arguments as well. In previous versions it was restricted to methods
|
|
|
|
without arguments.
|
2010-11-07 17:21:55 +08:00
|
|
|
|
|
|
|
A model with useful documentation might look like this::
|
|
|
|
|
|
|
|
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.")
|
2015-07-22 22:43:21 +08:00
|
|
|
author = models.ForeignKey(
|
|
|
|
User,
|
|
|
|
models.SET_NULL,
|
|
|
|
blank=True, null=True,
|
|
|
|
)
|
|
|
|
blog = models.ForeignKey(Blog, models.CASCADE)
|
2010-11-07 17:21:55 +08:00
|
|
|
...
|
|
|
|
|
|
|
|
def publish(self):
|
|
|
|
"""Makes the blog entry live on the site."""
|
|
|
|
...
|
|
|
|
|
|
|
|
View reference
|
|
|
|
==============
|
|
|
|
|
|
|
|
Each URL in your site has a separate entry in the ``admindocs`` page, and
|
|
|
|
clicking on a given URL will show you the corresponding view. Helpful things
|
|
|
|
you can document in your view function docstrings include:
|
|
|
|
|
2011-10-14 08:12:01 +08:00
|
|
|
* A short description of what the view does.
|
|
|
|
* The **context**, or a list of variables available in the view's template.
|
|
|
|
* The name of the template or templates that are used for that view.
|
2010-11-07 17:21:55 +08:00
|
|
|
|
|
|
|
For example::
|
|
|
|
|
2014-12-14 17:17:18 +08:00
|
|
|
from django.shortcuts import render
|
|
|
|
|
2010-11-07 17:21:55 +08:00
|
|
|
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`
|
|
|
|
"""
|
2014-12-14 17:17:18 +08:00
|
|
|
context = {'mymodel': MyModel.objects.get(slug=slug)}
|
|
|
|
return render(request, 'myapp/my_template.html', context)
|
2010-11-07 17:21:55 +08:00
|
|
|
|
|
|
|
Template tags and filters reference
|
|
|
|
===================================
|
|
|
|
|
|
|
|
The **tags** and **filters** ``admindocs`` sections describe all the tags and
|
|
|
|
filters that come with Django (in fact, the :ref:`built-in tag reference
|
|
|
|
<ref-templates-builtins-tags>` and :ref:`built-in filter reference
|
|
|
|
<ref-templates-builtins-filters>` 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.
|
|
|
|
|
|
|
|
|
|
|
|
Template reference
|
|
|
|
==================
|
|
|
|
|
|
|
|
While ``admindocs`` does not include a place to document templates by
|
|
|
|
themselves, if you use the ``:template:`path/to/template.html``` syntax in a
|
|
|
|
docstring the resulting page will verify the path of that template with
|
2010-11-20 19:10:48 +08:00
|
|
|
Django's :ref:`template loaders <template-loaders>`. This can be a handy way to
|
2010-11-07 17:21:55 +08:00
|
|
|
check if the specified template exists and to show where on the filesystem that
|
|
|
|
template is stored.
|
|
|
|
|
|
|
|
|
|
|
|
Included Bookmarklets
|
|
|
|
=====================
|
|
|
|
|
2015-07-13 04:37:17 +08:00
|
|
|
One bookmarklet is available from the ``admindocs`` page:
|
2010-11-07 17:21:55 +08:00
|
|
|
|
2011-10-14 08:12:01 +08:00
|
|
|
Documentation for this page
|
|
|
|
Jumps you from any page to the documentation for the view that generates
|
|
|
|
that page.
|
2010-11-07 17:21:55 +08:00
|
|
|
|
2015-07-13 04:37:17 +08:00
|
|
|
Using this bookmarklet requires that ``XViewMiddleware`` is installed and that
|
|
|
|
you are logged into the :mod:`Django admin <django.contrib.admin>` as a
|
2010-11-07 17:21:55 +08:00
|
|
|
:class:`~django.contrib.auth.models.User` with
|
2015-07-13 04:37:17 +08:00
|
|
|
:attr:`~django.contrib.auth.models.User.is_staff` set to ``True``.
|