Skip to content

Django templates derived from the view docstring for rapid prototyping

The rather verbose title says it all really.

We had a need the other week to churn out the skeleton of a site to see how the different areas fitted together.
As it was being written in Django anyway I put together this quick ‘n dirty utility that renders the reStructuredText docstring of a view to the returned response, so you can quickly put in page furniture and links to other views without having to go to the effort of creating templates.


def docview(fn):
    from docutils.core import publish_string
    from django.http import HttpResponse

    r = HttpResponse(publish_string(
            source=fn.func_doc,
            writer_name='html'))
    
    return lambda rtn: r

Then decorate your method:


@docview
def fast_view(request):
    """
===========
A Fast View
===========

With links to:

 * `An even faster view </faster_view>`_
 * `Somewhere else <http://isotoma.com>`_
 
    """

Which will render:

A Fast View

With links to: