1. 程式人生 > >搭建自己的部落格(四):優化首頁和詳情頁

搭建自己的部落格(四):優化首頁和詳情頁

上一篇簡單的建立了詳情頁和首頁,這篇稍微優化一下,添加發布日期,分類,標籤以及根據標籤篩選該標籤的內容。

1、優化首頁和詳情頁

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>{{ blog.title }}</title>
</head>
<body>

<a href="{% url 'home' %}">
    <div>
        <h3>felix Blog</h3>
    </div>
</a>
<h3>{{ blog.title }}</h3>
<p>作者:{{ blog.author }}</p>
{
# 時間過濾器讓時間按照自己需要的格式過濾 #} <p>釋出日期:{{ blog.created_time|date:"Y-m-d H:n:s" }}</p> <p>分類: <a href="{% url 'blogs_with_type' blog.blog_type.pk %}"> {{ blog.blog_type }} </a> </p> <p>{{ blog.content }}</p> </body> </html>
blog_detail.html
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>felix Blog</title>
</head>
<body>
<div>
    <h3>felix Blog</h3>
</div>
<br/>

{% for blog in blogs %}
    <a href="{% url 'blog_detail' blog.pk %}
"><h3>{{ blog.title }}</h3></a> {# 新增過濾器 文章太長時只顯示前30個字元 #} <p>{{ blog.content|truncatechars:30 }}</p> {% empty %} <p>--暫無部落格,敬請期待--</p> {% endfor %} {# 過濾器統計部落格數量 #} <p>一共有{{ blogs|length }}篇部落格</p> </body> </html>
blog_list.html

2、新增通過標籤篩選文章

在templates下新建blog_with_type.html檔案

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>{{ blog_type.type_name }}</title>
</head>
<body>
<div>
    <h3>felix Blog</h3>
</div>
<br/>
<h3>標籤名:{{ blog_type.type_name }}</h3>
{% for blog in blogs %}
    <a href="{% url 'blog_detail' blog.pk %}"><h3>{{ blog.title }}</h3></a>
    {#    新增過濾器 文章太長時只顯示前30個字元  #}
    <p>{{ blog.content|truncatechars:30 }}</p>
{% empty %}
    <p>--暫無部落格,敬請期待--</p>
{% endfor %}
{# 過濾器統計部落格數量 #}
<p>一共有{{ blogs|length }}篇部落格</p>
</body>
</html>
blog_with_type.html

3、新建檢視和路徑

def blogs_with_type(requests, blog_type_pk):
    blog_type = get_object_or_404(BlogType, pk=blog_type_pk)
    context = {
        'blogs': Blog.objects.filter(blog_type=blog_type),
        'blog_type':blog_type,
    }
    return render_to_response('blog_with_type.html', context)
from django.shortcuts import render_to_response, get_object_or_404
from .models import Blog, BlogType


# Create your views here.

# 部落格列表
def blog_list(requests):
    context = {
        'blogs': Blog.objects.all()
    }
    return render_to_response('blog_list.html', context)


# 部落格詳情
def blog_detail(requests, blog_pk):
    context = {
        'blog': get_object_or_404(Blog, pk=blog_pk)
    }

    return render_to_response('blog_detail.html', context)


def blogs_with_type(requests, blog_type_pk):
    blog_type = get_object_or_404(BlogType, pk=blog_type_pk)
    context = {
        'blogs': Blog.objects.filter(blog_type=blog_type),
        'blog_type':blog_type,
    }
    return render_to_response('blog_with_type.html', context)
views.py
# -*- coding: utf-8 -*-
# @Time    : 18-11-4 下午5:22
# @Author  : Felix Wang

from django.urls import path
from . import views
urlpatterns = [
    # name 表示別名
    path('<int:blog_pk>',views.blog_detail,name='blog_detail'),
    # 連結非常容易混掉,所以要區分
    path('type/<int:blog_type_pk>',views.blogs_with_type,name='blogs_with_type'),
]
urls.py

4、我的專案目錄

最近發現得新增自己的專案目錄,以分別出和上一篇部落格相比,那些檔案產生了變化。

其中:

  紅色表示新建的檔案

  藍色表示修改過得檔案

5、啟動服務測試

如果部落格資料不夠多,可以進入admin後臺管理中建立,比如多建立一些標籤和一些部落格,看效果。

6、其他只是補充

(1)、模板的常用過濾器

日期:date
字數擷取:truncatechars、truncatechars_html、truncatewords、truncatewords_html
是否信任html:safe
長度:length

參考連結:https://docs.djangoproject.com/en/2.1/ref/templates/builtins/

(2)、常用的模板標籤

迴圈:for
條件:if、ifequal、ifnotequal
連結:url
模板巢狀:block、extends、include
註釋:{#  #}