How to Use Markup in Django

October 23, 2006

Let me first say that I love Django. It’s been a hell of a development environment and the documentation is thorough and fantastic. Every once in a while, though, I can’t quite figure things out so I have to guess.

Here’s a simple guide to use Textile, Markdown and ReST in Django templates.

  • Put django.contrib.markup in your INSTALLED_APPS
  • Load markup in your templates via {% load markup %}
  • Filter whatever text with the appropriate filter: {{ text|textile }}

The Problem

I had to write this is because I thought this process wasn’t working. Textile wasn’t working because textile.py wasn’t installed. If the markup tag can’t import the proper parser it doesn’t throw an error, it just passes the value back.

So, to test if your setup environment is setup okay, test to see if your parser is installed. Fire up the Python interpreter and run:

Markdown: import markdown
Textile: import textile
ReST: from docutils.core import publish_parts

If you receive and ImportError, the parser is not installed.


Comments

I was having a bit of trouble with this also. I searched Google for "django textile" and your site had the info I needed. Thanks.

Posted by __wyatt

Thanks for this.. helped me out too!

Posted by Scott Benjamin

Thanks, this made it very easy to add markup.

Posted by Poromenos

This is a puzzle. I can import textile to the interpreter (when I just run python from the command line), but it fails in Django with this message. I can even run Python, import markup, and then run markup.textile('some text') and it works. But when I use the filter in a template, I get the error message saying that textile is not installed.

Any ideas?

Posted by Tim Barnes

Tim, have you made sure textile.py is in your python path? I believe that markup.textile will just return the string if the textile (or other) module required isn't installed.

Posted by SuperJared

Just to clarify things for other readers that may come.
http://pypi.python.org/pypi/textile/
This is the link where you have to *download* and *install* before using contrib.markup. It's a python library, not a django component.
I may be redundant but I didn't understand this by reading your post. ;)

Posted by carlocb

Add your comment

No HTML; Only URLs and line breaks are converted.