Django + uwsgi + nginx + bootstrap 創(chuàng)建自己的博客 -- 12.RSS和分頁

Django是一個全面型框架, 很多功能都可以直接找到, 對于RSS功能, 可以從其中的高層框架的聚合Feed框架中找到(The syndication feed framework)

上層Feed生成框架可以直接應(yīng)用Feed類, 我們可以直接繼承Feed在其中定義自己的方法

在my_blog/article/views.py中定義類

from django.contrib.syndication.views import Feed  #注意加入import語句


class RSSFeed(Feed) :
    title = "RSS feed - article"
    link = "feeds/posts/"
    description = "RSS feed - blog posts"

    def items(self):
        return Article.objects.order_by('-date_time')

    def item_title(self, item):
        return item.title

    def item_pubdate(self, item):
        return item.add_date

    def item_description(self, item):
        return item.content

然后在my_blog/my_blog/urls.py中設(shè)置對應(yīng)的url

from django.conf.urls import patterns, include, url
from django.contrib import admin
from article.views import RSSFeed

urlpatterns = patterns('',
    # Examples:
    # url(r'^$', 'my_blog.views.home', name='home'),
    # url(r'^blog/', include('blog.urls')),

    url(r'^admin/', include(admin.site.urls)),
    url(r'^$', 'article.views.home', name = 'home'),
    url(r'^(?P<id>\d+)/$', 'article.views.detail', name='detail'),
    url(r'^archives/$', 'article.views.archives', name = 'archives'),
    url(r'^aboutme/$', 'article.views.about_me', name = 'about_me'),
    url(r'^tag(?P<tag>\w+)/$', 'article.views.search_tag', name = 'search_tag'),
    url(r'^search/$','article.views.blog_search', name = 'search'),
    url(r'^feed/$', RSSFeed(), name = "RSS"),  #新添加的urlconf, 并將name設(shè)置為RSS, 方便在模板中使用url
)

下面修改my_blog/templates/base.html, 在其中添加RSS按鈕

<!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>
<body>
<div id="layout" class="pure-g">
    <div class="sidebar pure-u-1 pure-u-md-1-4">
        <div class="header">
            <h1 class="brand-title"><a href="{% url "home" %}">Andrew Liu Blog</a></h1>
            <h2 class="brand-tagline">雪憶 - Snow Memory</h2>
            <nav class="nav">
                <ul class="nav-list">
                    <li class="nav-item">
                        <a class="button-success pure-button" href="/">主頁</a>
                    </li>
                    <li class="nav-item">
                        <a class="button-success pure-button" href="{% url "archives" %}">歸檔</a>
                    </li>
                    <li class="nav-item">
                        <a class="pure-button" >Github</a>
                    </li>
                    <li class="nav-item">
                        <a class="button-error pure-button" >Weibo</a>
                    </li>
                    <li class="nav-item">
                        <a class="button-success pure-button" href="/">專題</a>
                    </li>
                    <li>
                    <form class="pure-form" action="/search/" method="get">
                    <input class="pure-input-3-3" type="text" name="s" placeholder="search">
                    </form>
                    </li>
                    <li class="nav-item">
                        <a class="button-success pure-button" href="{% url "about_me" %}">About Me</a>
                    </li>
                    <li class="nav-item">
                        <a class="button-success pure-button" href="{% url "RSS" %}">RSS</a>  
                    </li>
                </ul>
            </nav>
        </div>
    </div>


    <div class="content pure-u-1 pure-u-md-3-4">
        <div>
            {% block content %}
            {% endblock %}
            <div class="footer">
                <div class="pure-menu pure-menu-horizontal pure-menu-open">
                    <ul>
                        <li><a >About Me</a></li>
                        <li><a >Twitter</a></li>
                        <li><a >GitHub</a></li>
                    </ul>
                </div>
            </div>
        </div>
    </div>
</div>


</body>
</html>

保存后, 在瀏覽器中輸入http://127.0.0.1:8000/可以看到新增的RSS按鈕, 點擊看以看到對應(yīng)的效果

Mou icon
Mou icon

更多功能可以查看The syndication feed framework官方文檔

分頁功能

當博客文章較多的時候, 我們并不希望以此在主頁上顯示全部的博客文章, 而是希望能夠每頁顯示固定的文章數(shù)目, 這樣既能提高性能, 也能提高美觀度, 何樂而不為呢?

現(xiàn)在這一章節(jié)來看看怎么實現(xiàn)分頁功能

  • 首先添加包
  • 重寫home方法
  • 修改模板

修改my_blog/my_blog/views.py中的home函數(shù)

from django.core.paginator import Paginator, EmptyPage, PageNotAnInteger  #添加包

def home(request):
    posts = Article.objects.all()  #獲取全部的Article對象
    paginator = Paginator(posts, 2) #每頁顯示兩個
    page = request.GET.get('page')
    try :
        post_list = paginator.page(page)
    except PageNotAnInteger :
        post_list = paginator.page(1)
    except EmptyPage :
        post_list = paginator.paginator(paginator.num_pages)
    return render(request, 'home.html', {'post_list' : post_list})

修改my_blog/templates下的home.html

{% extends "base.html" %}

{% load custom_markdown %}
{% block content %}
<div class="posts">
    {% for post in post_list %}
        <section class="post">
            <header class="post-header">
                <h2 class="post-title"><a href="{% url "detail" id=post.id %}">{{ post.title }}</a></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|title }}</a>
                    </p>
            </header>

                <div class="post-description">
                    <p>
                        {{ post.content|custom_markdown|truncatewords:10 }}
                    </p>
                </div>
                <a class="pure-button" href="{% url "detail" id=post.id %}">Read More >>> </a>
        </section>
    {% endfor %}

    {% if post_list.object_list and post_list.paginator.num_pages > 1 %}
      <div>
      <ul class="pager">
      {% if post_list.has_previous %}
        <li><a href="?page={{ post_list.previous_page_number }}">上一頁</a></li>
      {% endif %}

      {% if post_list.has_next %}
        <li><a href="?page={{ post_list.next_page_number }}">下一頁</a></li>
      {% endif %}
      </ul>
      </div>
    {% endif %}
</div><!-- /.blog-post -->
{% endblock %}

我設(shè)置的是每頁顯示兩篇博文, 可以修改成其他數(shù)值

更多細節(jié)可以查看pagination官方文檔

保存后, 在瀏覽器中輸入http://127.0.0.1:8000/可以看到新增的下一頁按鈕(博客文章要多加幾篇), 點擊看以看到對應(yīng)的效果

到目前為止, 博客的基本功能都實現(xiàn)了, 下一篇我們將講到怎么在云端進行部署

最后我們把整個blog更新到github倉庫中

$  git add .  #添加全部更新到暫存區(qū)
$  git commit -m "rss and paginator"  #提交到git
[master b53356b] rss and paginator
 2 files changed, 24 insertions(+), 1 deletion(-)
$  git push  #提交到github中
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

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

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