Django + uwsgi + nginx + bootstrap 創(chuàng)建自己的博客 -- 9.多說,markdown和代碼高亮

添加多說

在Django1.5版本前是有內(nèi)置的評論系統(tǒng)的, 不過現(xiàn)在已經(jīng)放棄使用了, 在國內(nèi)比較常用的是多說, 在國外是disqus, 因?yàn)槲恼轮饕鎸?國內(nèi)用戶, 所以采用多說

在網(wǎng)站上注冊賬號或者直接用社交賬號進(jìn)行登錄,并會生成一個short_name, 可以在個人界面中的工具中找到一段通用代碼, 這段代碼非常重要, 用于多說評論框的代碼段:

請一定要把短名換成自己的多說短名, 非常感謝

<!-- 多說評論框 start -->
    <div class="ds-thread" data-thread-key="請將此處替換成文章在你的站點(diǎn)中的ID" data-title="請?zhí)鎿Q成文章的標(biāo)題" data-url="請?zhí)鎿Q成文章的網(wǎng)址"></div>
<!-- 多說評論框 end -->
<!-- 多說公共JS代碼 start (一個網(wǎng)頁只需插入一次) -->
<script type="text/javascript">
var duoshuoQuery = {short_name:"請?jiān)诖颂幪鎿Q成自己的短名"};
    (function() {
        var ds = document.createElement('script');
        ds.type = 'text/javascript';ds.async = true;
        ds.src = (document.location.protocol == 'https:' ? 'https:' : 'http:') + '//static.duoshuo.com/embed.js';
        ds.charset = 'UTF-8';
        (document.getElementsByTagName('head')[0] 
         || document.getElementsByTagName('body')[0]).appendChild(ds);
    })();
    </script>
<!-- 多說公共JS代碼 end -->

在templates中新建一個duoshuo.html并將代碼放入其中, 并做一些修改

<!-- 多說評論框 start -->
    <div class="ds-thread" data-thread-key="{{ post.id }}" data-title="{{ post.title }}" data-url="{{ post.get_absolute_url }}"></div>
<!-- 多說評論框 end -->
<!-- 多說公共JS代碼 start (一個網(wǎng)頁只需插入一次) -->
<script type="text/javascript">
var duoshuoQuery = {short_name:"andrewliu"};
    (function() {
        var ds = document.createElement('script');
        ds.type = 'text/javascript';ds.async = true;
        ds.src = (document.location.protocol == 'https:' ? 'https:' : 'http:') + '//static.duoshuo.com/embed.js';
        ds.charset = 'UTF-8';
        (document.getElementsByTagName('head')[0] 
         || document.getElementsByTagName('body')[0]).appendChild(ds);
    })();
    </script>
<!-- 多說公共JS代碼 end -->

然后在my_blog/article/models.py中重寫get_absolute_url方法

from django.db import models
from django.core.urlresolvers import reverse

# Create your models here.
class Article(models.Model) :
    title = models.CharField(max_length = 100)  #博客題目
    category = models.CharField(max_length = 50, blank = True)  #博客標(biāo)簽
    date_time = models.DateTimeField(auto_now_add = True)  #博客日期
    content = models.TextField(blank = True, null = True)  #博客文章正文

 #獲取URL并轉(zhuǎn)換成url的表示格式
    def get_absolute_url(self):
        path = reverse('detail', kwargs={'id':self.id})
        return "http://127.0.0.1:8000%s" % path

    def __str__(self) :
        return self.title

    class Meta:
        ordering = ['-date_time']

然后修改post.html

{% extends "base.html" %}
{% load custom_markdown %}

{% block content %}
<div class="posts">
        <section class="post">
            <header class="post-header">
                <h2 class="post-title">{{ post.title }}</h2>

                    <p class="post-meta">
                        Time:  <a class="post-author" href="#">{{ post.date_time|date:"Y /m /d"}}</a> <a class="post-category post-category-js" href="{% url "search_tag" tag=post.category %}">{{ post.category }}</a>
                    </p>
            </header>

                <div class="post-description">
                    <p>
                        {{ post.content|custom_markdown }}
                    </p>
                </div>
        </section>
        {% include "duoshuo.html" %}
</div><!-- /.blog-post -->
{% endblock %}

只需要將duoshuo.html加入到當(dāng)前頁面中, {% include "duoshuo.html" %}這句代碼就是將duoshuo.html加入到當(dāng)前的頁面中

現(xiàn)在啟動web服務(wù)器, 在瀏覽器中輸入http://127.0.0.1:8000/, 看看是不是每個博文頁面下都有一個多說評論框了..

Mou icon

markdown你的博文

markdown越來越流行, 越來越多的寫博客的博主都喜歡上了makrdown這種標(biāo)記性語言的易用性和美觀性. 像簡書, 作業(yè)部落, Mou都是比較出名的markdown在線或者離線形式

現(xiàn)在我們就來markdown自己的博客嗎, 首先是安裝markdown庫, 使用下面命令

#首先是安裝markdown
$ pip install markdown  #記得激活虛擬環(huán)境

現(xiàn)在說說怎么markdown你的博文, 在article下建立新文件夾templatetags,然后我們來定義的自己的 template filter, 然后在templatetags中建立init.py, 讓文件夾可以被看做一個包, 然后在文件夾中新建custom_markdown.py文件, 添加代碼

import markdown

from django import template
from django.template.defaultfilters import stringfilter
from django.utils.encoding import force_text
from django.utils.safestring import mark_safe

register = template.Library()  #自定義filter時必須加上


@register.filter(is_safe=True)  #注冊template filter
@stringfilter  #希望字符串作為參數(shù)
def custom_markdown(value):
    return mark_safe(markdown.markdown(value,
        extensions = ['markdown.extensions.fenced_code', 'markdown.extensions.codehilite'],
                                       safe_mode=True,
                                       enable_attributes=False))

然后只需要對需要進(jìn)行markdown化的地方進(jìn)行簡單的修改,

{% extends "base.html" %}
{% load custom_markdown %}

{% block content %}
<div class="posts">
        <section class="post">
            <header class="post-header">
                <h2 class="post-title">{{ post.title }}</h2>

                    <p class="post-meta">
                        Time:  <a class="post-author" href="#">{{ post.date_time|date:"Y /m /d"}}</a> <a class="post-category post-category-js" href="{% url "search_tag" tag=post.category %}">{{ post.category }}</a>
                    </p>
            </header>

                <div class="post-description">
                    <p>
                        {{ post.content|custom_markdown }}
                    </p>
                </div>
        </section>
        {% include "duoshuo.html" %}
</div><!-- /.blog-post -->
{% endblock %}

{% load custom_markdown %}添加自定義的filter, 然后使用filter的方式為{{ post.content|custom_markdown }}, 只需要對需要使用markdown格式的文本添加管道然后再添加一個自定義filter就好了.

現(xiàn)在啟動web服務(wù)器, 在瀏覽器中輸入http://127.0.0.1:8000/, 可以看到全新的的markdown效果

代碼高亮

這里代碼高亮使用一個CSS文件導(dǎo)入到網(wǎng)頁中就可以實(shí)現(xiàn)了, 因?yàn)樵谏厦鎸憁arkdown的filter中已經(jīng)添加了擴(kuò)展高亮的功能, 所以現(xiàn)在只要下載CSS文件就好了.

在pygments找到你想要的代碼主題, 我比較喜歡monokai, 然后在pygments-css下載你喜歡的CSS主題, 然后加入當(dāng)博客目錄的static目錄下, 或者最簡單的直接上傳七牛進(jìn)行CDN加速

修改base.html的頭部

<!doctype html>
<html lang="en">
<head>
    <meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta name="description" content="A layout example that shows off a blog page with a list of posts.">

    <title>{% block title %} Andrew Liu Blog {% endblock %}</title>
    <link rel="stylesheet" >
    <link rel="stylesheet" >
    <link rel="stylesheet" >
    <link rel="stylesheet" >
</head>

<link rel="stylesheet" >添加CSS樣式到base.html就可以了.

現(xiàn)在啟動web服務(wù)器, 添加一個帶有markdown樣式的代碼的文章, 就能看到效果了, 在瀏覽器中輸入http://127.0.0.1:8000/

Mou icon
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 添加多說 在Django1.5版本前是有內(nèi)置的評論系統(tǒng)的, 不過現(xiàn)在已經(jīng)放棄使用了, 在國內(nèi)比較常用的是多說, 在...
    阡陌3536閱讀 962評論 0 0
  • 點(diǎn)我查看本文集的說明及目錄。 本項(xiàng)目相關(guān)內(nèi)容( github傳送 )包括: 實(shí)現(xiàn)過程: CH1 創(chuàng)建一個博客應(yīng)用 ...
    學(xué)以致用123閱讀 1,611評論 1 3
  • 有一段時間沒有連續(xù)寫日記,斷掉的原因,是發(fā)現(xiàn)自己每天在成長進(jìn)步中,在固定的群體中去展示自己,同時,也稍微的學(xué)會的保...
    羅文均閱讀 160評論 0 0
  • 凡事第一次感覺都是怪怪的吧@_@
    奶絲兔米球閱讀 232評論 0 0
  • 問鼎·第二十七屆校園文化藝術(shù)節(jié)之同舟共濟(jì)協(xié)力 班級動員——強(qiáng)制組隊(duì)——還沒開始隊(duì)員傷退——臨危受命——賽前練習(xí)——...
    李鳳琴閱讀 154評論 0 0

友情鏈接更多精彩內(nèi)容