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 ...
随机推荐
- vue 组件内 directives指令的调用方式 <base-table v-auto-height:tableHeight="{vm:this, diffHeight:ahTable.diffHeight}"
vue 组件内 directives指令的调用方式 <base-table v-auto-height:tableHeight="{vm:this, diffHeight:ahTabl ...
- k8s部署测试实例
查看运行中pod,并运行一个容器 [root@mast-1 k8s]# kubectl get pods No resources found. [root@mast-1 k8s]# kubectl ...
- Linux之常用Shell脚本总结
一.简介本文将总结一些常用的shell脚本,方便以后工作中使用. 二.shell脚本[a]定期备份mysql数据库,需结合cronb定时任务调度实现. #!/bin/bash#首先声明一些自定义变量 ...
- No-4.文件和目录常用命令
文件和目录常用命令 结构 查看目录内容 ls 切换目录 cd 创建和删除操作 touch rm mkdir 拷贝和移动文件 cp mv 查看文件内容 cat more grep 其他 echo 重定向 ...
- 时钟Demo
其实是一个很简单的Demo,可以编译了拿NSIS打包.最近在做富文本编辑器和补C++不记得的东西吧,项目遥遥无期. //clock.pro #----------------------------- ...
- Java实现LRU(最近最少使用)缓存
package com.jd.test; import java.io.Serializable; import java.util.LinkedHashMap; import java.util.c ...
- Linux基础测试
目 录 第1章 文件及目录课后作业 1 第2章 Linux打包与压缩习题 1 第3章 Linux系统VIM编辑器习题 1 文件及目录课后作业 从/proc/meminfo中过滤出 ...
- 树梅派 -- 通过/sys读写ADC芯片 pcf8591
通过wiringPi等library, 在user space 通过/dev/i2c来读写i2c设备的方案不在本文讨论了. 编译SENSORS_PCF8591 模块 在Default raspberr ...
- Django框架基础知识09-请求与响应
视图函数接受到的request到底是个什么对象呢? HttpRequest对象: 服务器接收到http协议的请求后,会根据报文创建HttpRequest对象视图函数的第一个参数是HttpRequest ...
- 开发语言之---Python
Python,如果你想进军AI,或是不想被自动化运维淘汰,Python是一门必须课. 在未来的大学课堂上,也许也会将Python加入必修中,就像Java一样. Python之“Hello World” ...