Last active
August 29, 2015 14:01
-
-
Save Jwpe/d696b1a5dd4a6d81c85c to your computer and use it in GitHub Desktop.
Generating a rendered HTML field from a Markdown field on a model
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<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