上一篇博客介绍了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头像服务的更多相关文章

  1. django 简易博客开发 4 comments库使用及ajax支持

    首先还是贴一下源代码地址  https://github.com/goodspeedcheng/sblog 上一篇文章我们介绍了静态文件使用以及如何使用from实现对blog的增删改,这篇将介绍如何给 ...

  2. django 简易博客开发 3 静态文件、from 应用与自定义

    首先还是贴一下源代码地址  https://github.com/goodspeedcheng/sblog 上一篇博客我们介绍了 django 如何在views中使用templates以及一些常用的数 ...

  3. django 简易博客开发 2 模板和数据查询

    首先还是贴一下项目地址  https://github.com/goodspeedcheng/sblog   因为代码全在上面 上一篇博客我们介绍了 django的安装配置,新建project,新建a ...

  4. django 简易博客开发 1 安装、创建、配置、admin使用

    首先贴一下项目地址吧  https://github.com/goodspeedcheng/sblog 到现在位置项目实现的功能有: 1.后台管理使用Admin ,前端显示使用bootstrap 2. ...

  5. django 简易博客开发 1 安装、创建、配置、admin使用(转)

    Django 自称是“最适合开发有限期的完美WEB框架”.本文参考<Django web开发指南>,快速搭建一个blog 出来,在中间涉及诸多知识点,这里不会详细说明,如果你是第一次接触D ...

  6. Django个人博客开发 | 前言

    本渣渣不专注技术,只专注使用技术,不是一个资深的coder,是一个不折不扣的copier 1.前言 自学 Python,始于 Django 框架,Scrapy 框架,elasticsearch搜索引擎 ...

  7. [技术博客]React Native——HTML页面代码高亮&数学公式解析

    问题起源 原有博文显示时代码无法高亮,白底黑字的视觉效果不好. 原有博文中无法解析数学公式,导致页面会直接显示数学公式源码. 为了解决这两个问题,尝试了一些方法,最终利用开源类库实现了页面美化. (失 ...

  8. 简易博客开发(8)----django1.9 博客部署到pythonanywhere上

    准备工作 首先需要注册一下,pythonanywhere的免费账户有一定的限制,只能创建一个web app,不能绑定独立域名,不能通过ssh连接,不过只是搭一个project也是够用了. 注册成功之后 ...

  9. django中博客后台将图片上传作为用户头像

    添加上传目录 # 如果不添加上传目录,仍然可以上传成功,默认为project目录,如果models.py定义了upload_to="目录名称",则会上传到"project ...

随机推荐

  1. js运行机制(线程)

    浏览器线程 js运作在浏览器中,是单线程的,即js代码始终在一个线程上执行,这个线程称为js引擎线程. 浏览器是多线程的,除了js引擎线程,它还有:  UI渲染线程 浏览器事件触发线程 http请求线 ...

  2. Animate.css_css3动画库介绍

    插件描述:Animate.css内置了很多典型的css3动画,兼容性好使用方便. Animate.css是一个有趣的,跨浏览器的css3动画库.很值得我们在项目中引用. 用法 1.首先引入animat ...

  3. 原生查找DOM的方法

    JS获取DOM元素的方法(8种) 通过ID获取(getElementById) 通过name属性(getElementsByName) 通过标签名(getElementsByTagName) 通过类名 ...

  4. 主成分分析、因子分析、ICA(未完成)

    并且SVD分解也适用于一般的矩阵. 主成分分析可以简单的总结成一句话:数据的压缩和解释.常被用来寻找判断某种事物或现象的综合指标,并且给综合指标所包含的信息以适当的解释.在实际的应用过程中,主成分分析 ...

  5. onPullDownRefresh函数没有被正确执行

    原因 问题原因很多,我遇到的这个问题的原因是: 页面有两个同名的onPullDownRefresh函数,导致只执行最后的一个. 解决 只留一个onPullDownRefresh函数

  6. CodeForces - 1105D Kilani and the Game(多源BFS+暴力)

    题目: 给出一张游戏地图和每个玩家的位置,每次能移动的步数.p个玩家轮流移动占领地图中的格子(当格子已经被占领时就不能在占领了)在每个玩家都不能移动时游戏结束. 问在游戏结束后,每个玩家占领的格子的数 ...

  7. LINUX:关于Redis集群搭建 、和搭建项目中遇到的问题

    文章来源:http://www.cnblogs.com/hello-tl/p/7804225.html 0.Redis的简单安装 1.安装redis依赖 # yum install gcc tcl g ...

  8. python_random模块

    random模块 import random print(random.random()) # 大于0且小于1之间的小数 print(random.randint(1, 6)) # 大于等于1且小于等 ...

  9. 牛客网暑期ACM多校训练营(第四场) J 贪心

    链接: https://www.nowcoder.com/acm/contest/143/J #include<bits/stdc++.h> using namespace std; lo ...

  10. Oracle の ty_str_split + MySQL の proc_split

    oracle实现字符串分割 功能描述:用指定分隔符切割输入的字符串,返回一维数组,每个数组元素为一个子串. ); CREATE OR REPLACE FUNCTION fn_split (p_str ...