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/
django 简易博客开发 1 安装、创建、配置、admin使用
http://www.cnblogs.com/cacique/archive/2012/09/29/2707976.html
django 简易博客开发 2 模板和数据查询
http://www.cnblogs.com/cacique/archive/2012/09/30/2709143.html
django 简易博客开发 3 静态文件、from 应用与自定义
http://www.cnblogs.com/cacique/archive/2012/10/01/2709668.html
django 简易博客开发 4 comments库使用及ajax支持
http://www.cnblogs.com/cacique/archive/2012/10/03/2710803.html
django 简易博客开发 5 markdown支持、代码高亮、gravatar头像服务
http://www.cnblogs.com/cacique/archive/2012/10/07/2713703.html
django 简易博客开发 5 markdown支持、代码高亮、gravatar头像服务的更多相关文章
- django 简易博客开发 4 comments库使用及ajax支持
首先还是贴一下源代码地址 https://github.com/goodspeedcheng/sblog 上一篇文章我们介绍了静态文件使用以及如何使用from实现对blog的增删改,这篇将介绍如何给 ...
- django 简易博客开发 3 静态文件、from 应用与自定义
首先还是贴一下源代码地址 https://github.com/goodspeedcheng/sblog 上一篇博客我们介绍了 django 如何在views中使用templates以及一些常用的数 ...
- django 简易博客开发 2 模板和数据查询
首先还是贴一下项目地址 https://github.com/goodspeedcheng/sblog 因为代码全在上面 上一篇博客我们介绍了 django的安装配置,新建project,新建a ...
- django 简易博客开发 1 安装、创建、配置、admin使用
首先贴一下项目地址吧 https://github.com/goodspeedcheng/sblog 到现在位置项目实现的功能有: 1.后台管理使用Admin ,前端显示使用bootstrap 2. ...
- django 简易博客开发 1 安装、创建、配置、admin使用(转)
Django 自称是“最适合开发有限期的完美WEB框架”.本文参考<Django web开发指南>,快速搭建一个blog 出来,在中间涉及诸多知识点,这里不会详细说明,如果你是第一次接触D ...
- Django个人博客开发 | 前言
本渣渣不专注技术,只专注使用技术,不是一个资深的coder,是一个不折不扣的copier 1.前言 自学 Python,始于 Django 框架,Scrapy 框架,elasticsearch搜索引擎 ...
- [技术博客]React Native——HTML页面代码高亮&数学公式解析
问题起源 原有博文显示时代码无法高亮,白底黑字的视觉效果不好. 原有博文中无法解析数学公式,导致页面会直接显示数学公式源码. 为了解决这两个问题,尝试了一些方法,最终利用开源类库实现了页面美化. (失 ...
- 简易博客开发(8)----django1.9 博客部署到pythonanywhere上
准备工作 首先需要注册一下,pythonanywhere的免费账户有一定的限制,只能创建一个web app,不能绑定独立域名,不能通过ssh连接,不过只是搭一个project也是够用了. 注册成功之后 ...
- django中博客后台将图片上传作为用户头像
添加上传目录 # 如果不添加上传目录,仍然可以上传成功,默认为project目录,如果models.py定义了upload_to="目录名称",则会上传到"project ...
随机推荐
- js 逻辑运算符、等号运算符
1 逻辑运算符 逻辑运算只有2个结果,一个为true,一个为false. 1.且&& ★ 两个表达式为true的时候,结果为true. ------------------------ ...
- 尺取法 || POJ 2739 Sum of Consecutive Prime Numbers
给一个数 写成连续质数的和的形式,能写出多少种 *解法:先筛质数 然后尺取法 **尺取法:固定区间左.右端点为0,如果区间和比目标值大则右移左端点,比目标值小则右移右端点 ...
- sublime中使用markdown并实时编辑
1.需求 想在sublime中编辑.md文件 2.步骤 找到菜单栏: 快捷键,shift + command + p,选择 Package Control:Install Package, 没有找到P ...
- Go:获取命令行参数
一.Low B 方式 package main import ( "fmt" "os" ) func main() { fmt.Println("命令 ...
- HTML5表单新增元素与属性
form属性 在html4中,表单的从属元素必须写在表单内部,而在HTML5中,可以把他们书写在任何地方,然后为该元素指定一个form属性,属性值为该表单的id,这样就可以声明该元素从属于指定表单了. ...
- 开门人和关门人(结构体+sort)
每天第一个到机房的人要把门打开,最后一个离开的人要把门关好.现有一堆杂乱的机房签 到.签离记录,请根据记录找出当天开门和关门的人. Input 测试输入的第一行给出记录的总天数N ( > ...
- STL中set求交集、并集、差集的方法
并集(http://zh.cppreference.com/w/cpp/algorithm/set_union) 交集(http://zh.cppreference.com/w/cpp/algorit ...
- linux中的grep命令用法
原文请移驾:http://blog.csdn.net/greytree/article/details/428532 grep -- print lines matching a pattern (将 ...
- mac下出现xcrun: error导致git无法使用的解决办法
xcrun: error: invalid active developer path (/Library/Developer/CommandLineTools), missing xcrun BY ...
- js正则替换十六进制
var re=/\x62/;//没有0,也没有分号。alert(re.test("blue")); //output "true" 需要使用< 如需显示 ...