django 简易博客开发 5 markdown支持、代码高亮、gravatar头像服务

上一篇博客介绍了comments库使用及ajax支持,现在blog已经具备了基本的功能,但是只能发表文字,不支持富文本编辑。今天我们利用markdown添加富文本支持。

markdown语法说明: 

推荐第三个,可以直接在线编辑markdown文档。

django添加markdown支持

首先需要安装markdown  安装说明 : http://daringfireball.net/projects/markdown/

然后添加 django.contrib.markup 到 你的  INSTALLED_APPS 

现在在template添加  {% load markup %}

然后在你需要使用markdwon解析的地方更改为

{{ blog.content|markdown }}

下次添加博客的时候使用markdown语法书写,再次查看就能看到效果了

django 博客代码高亮

代码高亮工具有好多种 最常用的有syntaxhighlighter、Pygments,今天我们就来用Pygments实现代码高亮

首先安装pygments软件,使用如下代码:

sudo apt-get install python-pygments # ubuntu 

sudo yum install python-pygments # fedora 

然后运行如下命令

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

现在你会在在你的目录下找到code.css文件 放入css文件夹 在html中引入

 

{{ blog.content|markdown }}

 

改为

{{ blog.content|markdown:'codehilite' }}

添加博客的时候这样书写代码

    :::python
    print "hello world!"

注意:markdown的语法要求 代码前空四个空格 :::python声明是python代码 

 

gravatar头像服务

Gravatar(Globally Recognized Avatar的缩写) 是一项用于提供在全球范围内使用的头像服务。只要你在Gravatar的服务器上上传了你自己的头像,你便可以在其他任何支持Gravatar的博客、论坛等地方使用它。

我们将在评论种添加头像显示,让我们的评论更丰富一些

使用方法: 首先在sblog目录下新建目录 templatetags

然后新建文件gravatar.py  

# -*- coding: utf-8 -*-
### gravatar.py ###############
### place inside a 'templatetags' directory inside the top level of a Django app (not project, must be inside an app)
### at the top of your page template include this:
### {% load gravatar %}
### and to use the url do this:
### <img src="{% gravatar_url 'someone@somewhere.com' %}">
### or
### <img src="{% gravatar_url sometemplatevariable %}">
### just make sure to update the "default" image path below

from django import template
import urllib
import hashlib

register = template.Library()


class GravatarUrlNode(template.Node):
    def __init__(self, email):
        self.email = template.Variable(email)

    def render(self, context):
        try:
            email = self.email.resolve(context)
        except template.VariableDoesNotExist:
            return ''

        default = "http://127.0.0.1:8000/static/img/defaultavatar.png"
        size = 40

        gravatar_url = "http://www.gravatar.com/avatar/" + hashlib.md5(email.lower()).hexdigest() + "?"
        gravatar_url += urllib.urlencode({'d': default, 's': str(size)})

        return gravatar_url


@register.tag
def gravatar_url(parser, token):
    try:
        tag_name, email = token.split_contents()

    except ValueError:
        raise template.TemplateSyntaxError, "%r tag requires a single argument" % token.contents.split()[0]

    return GravatarUrlNode(email)

其中

default = "http://127.0.0.1:8000/static/img/defaultavatar.png"

是我设置的默认头像链接,可以自行替换成你自己的

在template种引入

{% load gravatar %}

在需要显示头像的地方加入

<img src="{% gravatar_url 'someone@somewhere.com' %}">

例如

<img class="gravatar" src="{% gravatar_url comment.user_email %}">

现在就完成了。

最后源代码可以在  https://github.com/goodspeedcheng/sblog 可以看一下 希望大家把错误的地方提出纠正一下。

                                                                                                                                                       谢谢

扩展阅读: https://docs.djangoproject.com/en/1.4/

推荐 Django 最佳实践 - 中文版  https://github.com/brantyoung/zh-django-best-practices/blob/master/readme.rst/

ps: 大四学生求实习 邮箱: cacique1103#gmail.com

django建议博客开发系列到这就算是结束了,剩下的大家自由发挥吧。

 

 

posted @ 2012-10-07 12:25  cacique  阅读(14058)  评论(7编辑  收藏  举报
Copyright ©2011 Goodspeed Cheng