上一篇博客介绍了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. gocode安装不上的解决办法

    mkdir -p $GOPATH/src/golang.org/x  //路径下创建此文件 cd $GOPATH/src/golang.org/x      //切换到此目录 git clone ht ...

  2. Java以组的数量将字符串分组

    package org.jimmy.autosearch2019.test; import java.util.ArrayList; public class Test20190327 { publi ...

  3. gcc 变量类型大小 练习 远离 cygwin64 需要带dll

    /* testmini.c -- very simple test program for the miniLZO library */ #include <stdio.h> #inclu ...

  4. 【收藏】史上最全的浏览器 CSS & JS Hack 手册

    浏览器渲染页面的方式各不相同,甚至同一浏览器的不同版本(“杰出代表”是 IE)也有差异.因此,浏览器兼容成为前端开发人员的必备技能.如果有一份浏览器 Hack 手册,那查询起来就方便多了.这篇文章就向 ...

  5. Echarts 异步数据加载遇到的问题

    看了Echarts官网异步加载数据的Demo var myChart = echarts.init(document.getElementById('main')); // 显示标题,图例和空的坐标轴 ...

  6. selenium 浏览器基础操作(Python)

    想要开始测试,首先要清楚测试什么浏览器.如何为浏览器安装驱动,前面已经聊过. 其次要清楚如何打开浏览器,这一点,在前面的代码中,也体现过,但是并未深究.下面就来聊一聊对浏览器操作的那些事儿. from ...

  7. MYSQL数据库攻防与加固

    这是“官方”原本的模样搬过来的..写的很粗略啊.还有篇详细的请查看:MySQL安全加固题目及答案参考解析 启动xserver-mysql,进入xserver-mysql,开始实验,实验步骤如下: 1. ...

  8. 大页(Huge Page)简单介绍

    x86(包括x86-32和x86-64)架构的CPU默认使用4KB大小的内存页面(getconf PAGESIZE),但是它们也支持较大的内存页,如x86-64系统就支持2MB大小的大页(huge p ...

  9. python书籍推荐:量化投资:以Python为工具

    所属网站分类: 资源下载 > python电子书 作者:mimi 链接:http://www.pythonheidong.com/blog/article/451/ 来源:python黑洞网 内 ...

  10. Javascript拼接HTML字符串的方法列举及思路

    转载过来,去掉一些废话吧. 目标: 方便的拼接字符串,不使用让人眼晕的+=.使用过程如下: 1,先创建一个作为“模板”的字符串,如:’My name is ${name},I\’m ${age}.’ ...