Posts Tagged Django

Syntax Highlighting in Django with Markdown and Pygments

I find Markdown to be a more readable and usable alternative to XHTML/CSS for formatting text, and I use it to format my articles at this Django-powered blog. When implementing syntax highlighting for code blocks within text, I searched for existing solutions and found many approaches that were too complicated and had shortcomings. After more research, I realized that syntax highlighting works out of the box in Django if you have a recent version of Markdown.

Here are the required steps to enable syntax highlighting in your Django application. First, install python-markdown version 2.0+ and python-pygments. Pygments is a syntax highlighter written in Python. Markdown 2.0+ has an extension system and comes with a syntax highlighting extension that uses Pygments. This extension is called CodeHilite. To use it, add the following to a Django template:

{% load markup %}
{{ text|markdown:'codehilite' }}

Next, you need to create a stylesheet that defines colors for syntax highlighting. To do so, run the following command:

$ pygmentize -S default -f html -a .codehilite > code.css

Include code.css in your template.

Now, to create a syntax-highlighted code block, indent the block by 4 spaces and declare the language of the block at the first line, prefixed by ::: (3 colons). This is better explained by example. The following text:

:::python
print 'Hello, World.'

Produces the following syntax-highlighted code block:

print 'Hello, World.'

Keep in mind that Markdown allows embedded HTML elements by default. You shouldn't enable this if the source of the text is untrusted. To disable HTML elements, use the following in your Django template instead:

{% load markup %}
{{ text|markdown:'safe,codehilite' }}

Pygments supports a long list of languages and styles. Be sure to check the demos too.

Blog Migrated to Django

I've been meaning to migrate my website from Drupal to Django for a very long time. Although Drupal is an excellent content management system, I got tired of working with PHP every time I wanted to add a feature or make a change. My previous web host didn't support Python so I had to stick with PHP. Recently however, I moved the website to a VPS at Linode and decided to migrate to Django as well.

Writing a blog application in Django took very little time thanks to the reusable apps that come with Django, like syndication, comments and admin. I also had to port the blog design to Django templates and migrate the content from HTML to Markdown. I've been using Markdown at StackOverflow and I really like it. It's concise, readable and much easier to work with in a text editor than HTML. I wrote a small script to convert existing articles from the subset of HTML that I was using to Markdown.

To run the website, I'm using Apache2/mod_wsgi for the backend, and nginx as a frontend. I chose mod_wsgi because it's very flexible. As for nginx, I chose it because it integrates nicely with StaticGenerator, a Django middleware that caches pages as files on local disk. StaticGenerator has an important advantage over using Memcached with Django: cached pages are served by nginx without hitting Django at all, so it's much faster. A quick benchmark on my setup showed that it was 8 times faster. StaticGenerator can only cache full pages, but this is fine for my needs.

The blog feed now contains full articles (as opposed to short summaries). This should be more convenient to those who read the blog via the feed.

"Django 1.0 Website Development" Released

My author copies of "Django 1.0 Website Development" have arrived. This is the second edition of my Django book. Django is a framework for building web applications in Python. This book explains how to assemble Django's features and take advantage of its power to design, develop, and deploy a fully-featured web site.

Django 1.0 Website Development

The new edition has been updated to Django 1.0. The key topics that the reader will learn from the book are:

  • Register users through a user authentication system and manage them efficiently.
  • Restrict user access to certain pages and protect against malicious input.
  • Create tags to allow site visitors to classify, view, and share content easily.
  • Create own administration interface for proper monitoring of the web site.
  • Enhance user interface with AJAX.
  • Enable voting and commenting on content, and display popular content to site visitors.
  • Build user networks; add friend management and invitation features for social networking.
  • Create unit tests to automate the testing of code.

The full table of contents is available.

The book is available in paper and PDF formats at Packt Publishing. It is also available from all major book sellers like Amazon.

Writing the book and revising it have been an enjoyable experience for me. The feeling of accomplishment when my copies arrived is satisfying. I sincerely hope that readers find the book interesting and useful. If you have questions or comments, don't hesitate to email me!

More photos of the book are available at my Picasa web albums.