Django个人网站中加入Markdown编辑功能
Markdown真是个好东西!
Markdown是一种文本语言,用它写好的东西,具有简单清晰的样式。它可将text转化成html或者xhtml。
与html相比,它具有更简单的语法,基本上写上一个简单的文档就可以入手。
安装markdown
这个可以看成是将markdown转化成html的工具。而想把markdown文本以html的格式显示到页面上还需要下面的django-markdown-deux。
执行sudo sudo pip install markdown
。
这是一个将Markdown语言转换成html的库。
测试一下:
cslzy (deploy *) lazy $ python
Python 2.7.10 (default, Oct 23 2015, 19:19:21)
[GCC 4.2.1 Compatible Apple LLVM 7.0.0 (clang-700.0.59.5)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> from markdown import markdown
>>> test = '''
... #This is a test header
... ##The second line
... table|column
... ----|----
... python|2.7.x
... django|1.9.x
... ##Is this work?
... '''
>>> test
'\n#This is a test header\n##The second line\ntable|column\n----|----\npython|2.7.x\ndjango|1.9.x\n##Is this work?\n'
>>> markdownText = markdown(test)
>>> markdownText
u'<h1>This is a test header</h1>\n<h2>The second line</h2>\n<p>table|column\n----|----\npython|2.7.x\ndjango|1.9.x</p>\n<h2>Is this work?</h2>'
>>>
可以看到,确实是将标题转换成功了,不过貌似对表格的支持并不好。总之先试试。
测试
用一个简单的网页把Article类的结果返回出来。
页面:
<! --show my articles -->
<!DOCTYPE html>
<html>
<head>
<title>Show Articles</title>
{% load staticfiles %}
<link rel="stylesheet" href="{% static 'article/css/main.css' %}" type="text/css">
</head>
<body>
<h1 align="center">Title: <strong>{{ title }}</strong></h1>
<h5>Category: <strong>{{ category }}</strong></h5>
<h5>Date&Time: <strong>{{ date }}</strong></h5>
<br/>
<h2>Content</h2>
<strong>{{ content }}</strong>
</body>
</html>
后台:
def detail(request, args):
try:
article_id = int(args)
except:
return HttpResponse("Invalid Article Number...")
article_list = Article.objects.all()
if article_id not in range(1, len(article_list)+1):
return HttpResponse("Invalid Article Number...")
atc = Article.objects.get(id=article_id)
template = loader.get_template('show_atc.html')
#return_dict = {'title': atc.title, 'category': atc.category, 'date': atc.date_time, 'content': atc.content}
return_dict = {'title': atc.title, 'category': atc.category, 'date': atc.date_time, 'content': markdown(atc.content)} # 此处文章的内容转化成html
#return HttpResponse(template.render(return_dict, request)) #返回并查看结果
return HttpResponse(markdown(atc.content))
使用<strong>{{ content }}</strong>
会直接将转化的html语句输出到页面上。
使用django-markdown-deux
找到一篇文章使用的django-markdown-deux
安装一下,
pip install django-markdown-deux
在settings.py中添加
INSTALLED_APPS = [
...
'markdown_deux',
...
]
在页面中载入模块
{% load markdown_deux_tags %}
...
<p>{{ entry.body|markdown }}</p>
嗯,这样就可以了。
测试:将文件显示到网页中。
我画个表:
table | column |
---|---|
python | 2.7.x |
django | 1.9.x |
通过下面的代码,我把这篇文章的.md文件加载到页面中。
显示基本还算正常,但表格显示有问题。
def detail(request, args):
# detect if args is well-format
try:
article_id = int(args)
except:
return HttpResponse("Invalid Article Number...")
article_list = Article.objects.all()
if article_id not in range(1, len(article_list)+1):
return HttpResponse("Invalid Article Number...")
atc = Article.objects.get(id=article_id)
template = loader.get_template('show_atc.html')
# show markdown text
mfile = open('./../../django-markdown-deux.md', 'r').read()
atc.content = mfile
return_dict = {'title': atc.title, 'category': atc.category, 'date': atc.date_time, 'content': atc.content}
return HttpResponse(template.render(return_dict, request))
先到这,后面再慢慢优化。