1. 程式人生 > >Django文章標題列表優化

Django文章標題列表優化

一 目標

為文章標題列表新增作者的相關資訊。

二 編寫檢視函式mysite/article/list_views.py

from django.shortcuts import render, get_object_or_404
from django.core.paginator import Paginator, EmptyPage, PageNotAnInteger
from django.contrib.auth.models import User

from .models import ArticlePost

# 這種方式兼顧兩種可能,一種的直接請求所有文章標題,另外一個是請求某個使用者的文章標題
def article_titles(request, username=None):
    # 檢視某一個作者的文章進次分支
    if username:
        user = User.objects.get(username=username)
        articles_title = ArticlePost.objects.filter(author=user)
        try:
            userinfo = user.userinfo
        except:
            userinfo = None
    else:
        articles_title = ArticlePost.objects.all()
    # 每頁展示4篇文章
    paginator = Paginator(articles_title, 4)
    # 獲得要展示的頁面
    page = request.GET.get('page')
    try:
        # 獲取當前頁
        current_page = paginator.page(page)
        # 獲得當前頁的文章物件
        articles = current_page.object_list
    except PageNotAnInteger:
        current_page = paginator.page(1)
        articles = current_page.object_list
    except EmptyPage:
        current_page = paginator.page(paginator.num_pages)
        articles = current_page.object_list
    if username:
        # 傳遞使用者相關資訊給前端
        return render(request, "article/list/author_articles.html", {"articles":articles, "page":current_page, "userinfo":userinfo, "user":user})
    return render(request, "article/list/article_titles.html", {"articles":articles, "page": current_page})

# 沒有登入的使用者也可以訪問這個函式
def article_detail(request, id, slug):
    article = get_object_or_404(ArticlePost, id=id, slug=slug)
    return render(request, "article/list/article_detail.html", {"article":article, })

三 新增模板mysite/templates/article/list/author_articles.html

{% extends "base.html" %}
{% load staticfiles %}
{% block title %}文章{% endblock %}
{% block content %}
<div class="row text-center vertical-middle-sm">
    <h1>閱讀,豐富頭腦,善化行為</h1>
</div>
<div class="container">
<div class="col-md-8">
    {% for article in articles %}
    <div class="list-group">
        <a href="{{article.get_url_path}}" class="list-group-item active"><h4 class="list-group-item-heading">{{article.title}}</h4></a>
          <p class="list-group-item-text">作者:<a href="{% url 'article:author_articles' article.author.username %}">{{article.author.username}}</a></p>
           <p class="list-group-item-text">概要:{{article.body|slice:'70'|linebreaks}}</p>
    </div>
    {% endfor %}
{% include "paginator.html" %}
</div>
<div class="col-md-4">
<div>
    <!--顯示作者的圖片-->
    {% if userinfo.photo %}
    <img src="{{ userinfo.photo | striptags }}" class="img-circle" id="my_photo" name="user_face">
    {% else %}
    <img name="user_face" src="{% static 'images/newton.jpg' %}" class="img-circle" id="my_photo">
    {% endif %}
</div>
<div>
    <!--顯示使用者相關資訊-->
    <p>{{ user.username }}</p>
    {% if userinfo %}
    <p>{{ userinfo.company }}
    <p>{{ userinfo.aboutme }}</p>
{% else %}
<p>這個作者太懶了,什麼也沒有留下。</p>
{% endif %}
</div>
</div>
</div>
{% endblock %}

四 測試結果