django初探-建立簡單的博客系統(二)

  上篇 django初探-建立簡單的博客系統(一)已經記錄了Django實現博客的發佈的整個過程,接下來繼續說明博客標題和內容的顯示。

顯示博客信息

  將博客內容保存到數據庫還不是發佈博客的終極目的,博客必定要顯示出來,顯示博客信息的基本知識以下圖:css

  

顯示文章標題

獲取數據

  當咱們在./blog/models.py中建立了數據模型後,Django就會自動提供數據庫抽象的API,經過API能夠建立、獲取、修改或刪除對象html

  咱們能夠使用交互模式進行測試python

  python manage.py shelljquery

  

  對BlogArticles類能夠進行相似操做git

  

添加模板

  在/blog下templates文件夾,在templates文件夾下添加base.html、titile.html和content.html三個文件github

  base.htmlshell

<!DOCTYPE html>
<html lang="zh-cn">
    <head>
        <meta http-equiv="X-UA_compatible" content="IE=Edge">
        <meta charset="utf-8">
        <meta name="viewport" content="width=device-width, initial-scale=1">
        <title>
            {%block title%}
            {%endblock%}
        </title>
        <link rel="stylesheet" href="http://necolas.github.io/normalize.css/">
        <link rel="stylesheet" href="https://cdn.bootcss.com/bootstrap/3.3.7/css/bootstrap.min.css">
    </head>
    <body>
        <div class="container">
            {%block content%}
            {%endblock%}
        </div>
        <scripy src="https://cdn.bootcss.com/jquery/3.2.1/jquery.js"></scripy>
        <scripy src="https://cdn.bootcss.com/bootstrap/3.3.7/js/bootstrap.min.js"></scripy>
    </body>
</html>

  titles.html數據庫

{%extends "base.html"%}

{%block title%}blog titles{%endblock%}

{%block content%}
<div class="row text-center vertical-middle-sm">
    <h1>小兵千睿</h1>
</div>
<div class="row">
    <div class="col-xs-12 c0l-md-8">
        <ul>
            {%for blog in blogs%}
                <li><a href="{{blog.id}}">{{blog.title}}</a></li> <!--雙層花括號表示此處顯示變量引用的數據-->                
            {%endfor%}
        </ul>
    </div>
    <div class="col-xs-6 col-md-4">
        <h2>show time</h2>
        <img width="200px" src="http://images.cnblogs.com/cnblogs_com/xiaobingqianrui/1185116/o_Image%201.png">
    </div>
</div>
{%endblock%}

  content.htmldjango

{%extends "base.html"%}

{%block title%}blog article{%endblock%}

{%block content%}
<div class="row text-center vertical-middle-sm">
    <h1>{{article.title}}</h1>
</div>
<div class="row">
    <div class="col-xs-12 c0l-md-8">
        <p class="text-center"><span>{{article.author}}</span>
        <span style="margin-left:20px">{{publish}}</span></p>
        <div>{{article.body}}</div>
    </div>
    <div class="col-xs-6 col-md-4">
        <h2>show time</h2>
        <img width="200px" src="http://images.cnblogs.com/cnblogs_com/xiaobingqianrui/1185116/o_Image%201.png">
    </div>
</div>
{%endblock%}

添加視圖函數

  /blog/view.py中添加相應代碼bootstrap

from django.shortcuts import render

# Create your views here.
from .models import BlogArticles
from django.shortcuts import render

def blog_title(request):
    blogs = BlogArticles.objects.all()
    return render(request, 'titles.html', {"blos":blogs})

def blog_article(request, articls_id):
    article = BlogArticles.objects.get(id=articls_id)
    pub = article.publish
    return render(request, 'content.html', {"article":article, "publish":pub})

添加應用URL

  1. /xbqr/urls.py中添加代碼

from django.contrib import admin
from django.urls import path
from django.conf.urls import url,include

urlpatterns = [
    path('admin/', admin.site.urls),
    url(r'^blog/', include(('blog.urls',"blog"),namespace="blog")),
]

  2. 在/blog/目錄下新建urls.py文件,添加以下代碼

from django.conf.urls import url
from . import views

urlpatterns = [url(r'^$', views.blog_title, name="blog_title"),
                url(r'(?P<article_id>\d)/$', views.blog_article, name="blog_detail"),]

顯示文章內容

  顯示文章內容代碼在views.py中已添加

def blog_article(request, articls_id):
    article = BlogArticles.objects.get(id=articls_id)
    pub = article.publish
    return render(request, 'content.html', {"article":article, "publish":pub})

  啓動服務器:python manage.py runserver

  打開http://127.0.0.1:8000/blog/

  

  點擊標題查看文章內容

  

設置文章列表頁

  到目前爲止,博客標題和內容的顯示就完成了,附加添加如何設置發佈博客頁面的文章列表頁

  

  這樣顯示太過於單一,爲了讓其顯示的更加豐富,能夠在/blog/admin.py文件中添加以下代碼:

from django.contrib import admin

from .models import BlogArticles
# Register your models here.

class BlogArticlesAdmin(admin.ModelAdmin):
    list_display = ("title", "author", "publish")
    list_filter = ("publish", "author")
    search_fields = ("title", "body")
    raw_id_fields = ("author",)
    date_hierarchy = "publish"
    ordering=["publish","author"]

admin.site.register(BlogArticles, BlogArticlesAdmin)  

  刷新頁面:

相關文章
相關標籤/搜索