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 ...
随机推荐
- 指针-AC自动机
大家都不喜欢指针,但是这个AC自动机仿佛不用不行…… 先引用我最喜欢的话:“AC自动机,不是自动AC的机器.” 如果写不好还可能一直WA AC自动机是KMP与Trie树的完美结合,适用于多字符串匹配, ...
- 在.vue文件中让html代码自动补全的方法(支持vscode)
在.vue文件中让html代码自动补全的方法(支持vscode) https://blog.csdn.net/qq_36529459/article/details/79196763 "fi ...
- Eclipse 下载 开源项目 maven依赖丢失和 Deployment Assembly 丢失
周末下载了最新的jeecg的源码来瞅瞅,但是下载后发现,pom文件中定义的依赖都丢失了. 如下图 上网搜索了一下啊,发现需要先给这个项目这个项目 disable maven nature 然后再添加上 ...
- Vue之数据绑定
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- Linux下安装Redis5.0.2
1.下载redis 地址 http://download.redis.io/releases/redis-5.0.2.tar.gz 2.解压tar -zxf redis-5.0.2.tar.gz 3. ...
- python中的参数、全局变量及局部变量
1.位置参数.关键字参数.默认参数的使用 位置参数.关键字参数 def test(x,y,z): print(x) print(y) print(z) test(1,2,3) #位置参数,必须一一对应 ...
- nginx如何防止高负载造成服务器崩溃
nginx-http-sysguard模块 一.作用 防止因nginx并发访问量过高或者遭受攻击造成服务器宕机,可根据负载设置界面跳转. 二.安装配置 1.下载模块软件包 wget https:/ ...
- day17-python之文件操作
1.内置函数 #!/usr/bin/env python # -*- coding:utf-8 -*- # print(abs(-1)) # print(abs(1)) # # print(all([ ...
- 阿里云配置tomcat后不能访问问题
问题:使用阿里云centos 7.2配置好tomcat后,启动时间9分多钟,停在webapps下的manage这里近9分多钟 解决:进入 /usr/local/jdk1.8.0_144/jre/lib ...
- HTML元素的基本特性
1,Disabled 特性: //Disabled 设置元素不可用: $(this).attr("disabled","disabled") //移除push元 ...