Search code examples
pythonpython-sphinx

Shorter name for the class link in Sphinx


.. seealso::

   Class :class:`apps.business.models.Department`
      Explanation goes here

   `GNU tar manual, Basic Tar Format <http://link>`_
      Documentation for tar archive files, including GNU tar extensions.

That creates a proper link to the class Department.

enter image description here

How do I change the HTML output to have a just a single class name, without 'apps.business.models' prefix?

so it reads Class Department in HTML?


Solution

  • There is the ~ (tilde) in standard cross-referencing syntax.

    If you prefix the content with ~, the link text will only be the last component of the target. For example,

    :py:meth:`~Queue.Queue.get`
    

    will refer to Queue.Queue.get but only display get as the link text. This does not work with all cross-reference roles, but is domain specific.

    There is also the directive .. currentmodule:: with some documentation and an example in another SO answer.