Skip to content

Instantly share code, notes, and snippets.

@Jwpe
Last active August 29, 2015 14:01
Show Gist options
  • Save Jwpe/d696b1a5dd4a6d81c85c to your computer and use it in GitHub Desktop.
Save Jwpe/d696b1a5dd4a6d81c85c to your computer and use it in GitHub Desktop.
Generating a rendered HTML field from a Markdown field on a model
from django.db import models
import markdown
class Blog(models.Model):
content = models.TextField()
_html = models.TextField()
def save(self, *args, **kwargs):
"""
Render the Markdown content to HTML and store it in the _html field.
"""
self._html = markdown.markdown(self.content, safe_mode='escape')
super(Blog, self).save(*args, **kwargs)
<html>
...
<div class="md-content">
{{ blog._html|safe }}
</div>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment