搭建本身的博客(八):使用fontawesome框架來添加圖標以及美化詳情頁

在網頁中有時候會使用到圖標,本身弄圖標有些麻煩因此就使用了fontawesome框架。css

官網:   下載地址html

我使用的fontawesome版本是5.5.0版本web

一、先上變化的部分django

二、解釋app

圖中fontawesome文件夾是從官網下載的,能夠去下載:下載地址框架

三、變化文件內容ide

ul.blog-types{
    list-style-type: none;
}

div.blog:not(:last-child){
    margin-bottom: 2em;
    padding-bottom: 1em;
    border-bottom: 1px solid #eee;
}

div.blog h3{
    margin-top: 0.5em;
}

div.blog-info p{
    margin-bottom: 0;
}

div.blog-info-description{
    list-style-type: none;
    margin-bottom: 1em;
}

ul.blog-info-description li{
    display: inline-block;
    margin-right: 2em;
}

div.blog-content{
    text-indent: 2em;
}
blog.css
{# 引用模板 #}
{% extends 'base.html' %}

{% load staticfiles %}
{% block header_extends %}
    <link rel="stylesheet" href="{% static 'blog/blog.css' %}">
{% endblock %}


{# 標題 #}
{% block title %}
    {{ blog.title }}
{% endblock %}

{# 內容#}
{% block content %}
    <div class="container">
        <div class="row">
            <div class="col-6 offset-1">
                <ul class="blog-info-description">
                    <h3>{{ blog.title }}</h3>
                    <li>做者:{{ blog.author }}</li>
                    {# 時間過濾器讓時間按照本身須要的格式過濾 #}
                    <li>發佈日期:{{ blog.created_time|date:"Y-m-d H:n:s" }}</li>
                    <li>分類:
                        <a href="{% url 'blogs_with_type' blog.blog_type.pk %}">
                            {{ blog.blog_type }}
                        </a>
                    </li></ul>
                    <p class="blog-content">{{ blog.content }}</p>

            </div>
        </div>
    </div>


{% endblock %}

{% block js %}
    <script>
        $(".nav-blog").addClass("active").siblings().removeClass("active");
    </script>
{% endblock %}
blog_detail.html
{% extends 'base.html' %}
{% load staticfiles %}
{# 標題 #}
{% block title %}
    felix Blog
{% endblock %}

{% block header_extends %}
    <link rel="stylesheet" href="{% static 'blog/blog.css' %}">
    <link rel="stylesheet" href="{% static 'fontawesome-free-5.5.0-web/css/all.min.css' %}">
{% endblock %}

{# 內容#}
{% block content %}
    <div class="container">
        <div class="row">
            <div class="col-md-8">
                <div class="card" style="">
                    <div class="card-header"><h5 class="card-title">{% block blog_type_title %}博客列表(一共有
                        {{ blogs|length }}篇博客){% endblock %}</h5></div>
                    <div class="card-body">
                        {% for blog in blogs %}
                            <div class="blog">
                                <h3><a href="{% url 'blog_detail' blog.pk %}">{{ blog.title }}</a></h3>
                                <div class="blog-info">
                                    <p>
                                        {# 添加圖標 #}
                                        <i class="fas fa-tag"></i>
                                        <a href="{% url 'blogs_with_type' blog.blog_type.pk %}">
                                            {{ blog.blog_type }}
                                        </a>
                                        <i class="far fa-clock  "></i>
                                        {{ blog.created_time|date:"Y-m-d" }}
                                    <p>
                                </div>
                                <p>{{ blog.content|truncatechars:30 }}</p>
                            </div>
                        {% empty %}
                            <div class="blog">
                                <h3>--暫無博客,敬請期待--</h3>
                            </div>
                        {% endfor %}
                    </div>
                </div>
            </div>
            <div class="col-md-4">
                <div class="card" style="">
                    <div class="card-header"><h5 class="card-title">博客分類</h5></div>
                    <div class="card-body">
                        <ul class="blog-types">
                            {% for blog_type in blog_types %}
                                <li><a href="{% url 'blogs_with_type' blog_type.pk %}">{{ blog_type.type_name }}</a>
                                </li>
                            {% empty %}
                                <li>暫無分類</li>
                            {% endfor %}
                        </ul>
                    </div>
                </div>
            </div>
        </div>
    </div>
{% endblock %}

{% block js %}
    <script>
        $(".nav-blog").addClass("active").siblings().removeClass("active");
    </script>
{% endblock %}
blog_list.html
{% extends 'blog/blog_list.html' %}

{# 標題 #}
{% block title %}
    {{ blog_type.type_name }}
{% endblock %}

{% block blog_type_title %}
    分類:{{ blog_type.type_name }}(一共有{{ blogs|length }}篇博客)
{% endblock %}
blog_with_type.html
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(),
        'blog_types':BlogType.objects.all(),
    }
    return render_to_response('blog/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/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,
        'blog_types': BlogType.objects.all(),
    }
    return render_to_response('blog/blog_with_type.html', context)
views.py

四、注意點梳理url

(1)、關於靜態文件和模板文件的命名空間問題。spa

默認尋找是先從全局找,而後到每一個app中找,若是直接放在app中的static和templates裏面,django找不到,由於先從全局找了。因此我在app中新建了一個blog文件夾,把文件和模板放在新建的文件夾中。code

(2)、fontawesome很是好用,要多查文檔:fontawesome文檔

相關文章
相關標籤/搜索