http://blog.csdn.net/feelang/article/details/25626725
markdown
比wiki語法要簡單,github上的代碼幾乎都是readme.md,這種易讀易寫的腳本語言已經變得非常流行.
本片博文簡單介紹一下如何在Django中集成markdown的功能
下載python下的markdown解析器.
$sudo pip install markdown
或者是
$sudo easy install markdown
在app的models.py中創建一個博文的類Post
-
class Category(models.Model):
-
name = models.CharField(max_length=20)
-
-
def __unicode__(self):
-
return self.name
-
-
-
class Post(models.Model):
-
category = models.ForeignKey(Category)
-
title = models.CharField(max_length=60)
-
body = models.TextField()
-
date = models.DateTimeField(auto_now_add=True)
-
-
def __unicode__(self):
-
return self.title
在view中使用markdown
導入包
-
from markdown import markdown
解析post.body
-
for post in posts:
-
post.body = markdown(post.body)
後臺編輯博文
添加如下內容
> ## This is a header.
>
> 1. This is the first list item.
> 2. This is the second list item.
>
> Here's some example code:
>
> return shell_exec("echo $input | $markdown_script");
前端顯示效果