1. 程式人生 > >簡單的部落格系統(四)Django請求HTML頁面檢視資訊--基於函式的檢視

簡單的部落格系統(四)Django請求HTML頁面檢視資訊--基於函式的檢視

1. 編寫用於查詢資料的功能函式

應用目錄 下的 views.py 檔案通常用於儲存響應各種請求的函式或類

from django.shortcuts import render
from .models import BlogArticles

# Create your views here.
def blog_title(request): # request 負責響應所接收到的請求
    # 查詢得到所有 BlogArticles 表中的所有例項資料
    blogs = BlogArticles.objects.all()
    """
    render() 作用是將資料渲染到指定模板上
    "blog/titles.html" 指定渲染到哪個頁面,頁面路徑為應用目錄下的templates目錄
    {"blogs": blogs} 為傳給頁面的資料
    """
    return render(request, "blog/titles.html", {"blogs": blogs})

2. 在應用目錄下建立相關html頁面檔案

  • 檔案目錄結構如下:

  • 編寫相關頁面程式碼
<!-- base.html公共模板頁面 -->
<!DOCTYPE html>
<html lang="zh-CN">
  <head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>{% block title %}{% endblock %}</title> <!-- 表示裡面的內容被 title block 代替 -->

    <!-- Bootstrap -->
    <link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet">

    <!--[if lt IE 9]>
      <script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/html5shiv.min.js"></script>
      <script src="https://cdn.jsdelivr.net/npm/[email protected]/dest/respond.min.js"></script>
    <![endif]-->
  </head>
  <body>
    <div class="container">
        {% block content %} <!-- 表示頁面的內容被 content block 代替 -->
        {% endblock %}
    </div>
    <script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/jquery.min.js"></script>
    <script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.min.js"></script>
  </body>
</html>

<!-- titles.html -->
{% extends "base.html" %} <!-- 繼承 base.html 模板頁 -->

{% block  title %}
部落格標題
{% endblock %}

{% block content %}
    <div class="row text-center vertical-middles-sm">
        <h1>我的部落格</h1>
    </div>
    <div class="row">
        <div class="col-xs-12 col-md-8">
            <ul>
                <!-- 迴圈遍歷 blogs 裡的物件 -->
                {% for blog in blogs %}
                    <li>{{ blog.title }}</li>
                {% endfor %}
            </ul>
        </div>
    </div>
{% endblock %}

3. 配置URL

  1. 編寫 專案目錄 下的 urls.py 檔案
from django.contrib import admin
from django.urls import path, include

urlpatterns = [
    path('admin/', admin.site.urls),
    path("blog/", include('blog.urls')), # 通過這裡將blog/請求轉向blog應用的urls.py,即./blog/urls.py檔案
  1. 編寫 應用目錄 下的 urls.py 檔案
from django.urls import path
from . import views

"""
第一個引數為空,表示訪問根,因為該檔案在blog應用中,則要為blog/
views.blog_title 表示響應該請求的函式
"""
urlpatterns = [
    path('', views.blog_title),
]
  1. 啟動專案訪問:http://127.0.0.1:8000/blog/ 即可看到結果

4. 給url傳引數

  1. 修改 titles.html 檔案給文章標題新增連結
<!-- 迴圈遍歷 blogs 裡的物件 -->
{% for blog in blogs %}
<li><a href="{{ blog.id }}">{{ blog.title }}</a></li>
{% endfor %}
  1. 為該請求編寫對應的函式(./blog/views.py)
def blog_article(request, article_id): # 該請求傳入 article_id 引數
    article = BlogArticles.objects.get(id=article_id)
    pub = article.publish
    return render(request, "blog/content.html",{"article":article, "publish":pub})
  1. 編寫顯示檔案內容的html頁面(templates/blog/content.html)
{% extends "base.html" %} <!-- 繼承 base.html 模板頁 -->

{% block  title %}
部落格標題
{% endblock %}

{% block content %}
    <div class="row text-center vertical-middles-sm">
        <h1>{{ article.title }}</h1>
    </div>
    <div class="row">
        <div class="col-xs-12 col-md-8 col-md-offset-2">
            <p class="text-center">
                <span>{{ article.author.username }}</span>
                <span style="margin-left: 20px;">{{ publish }}</span>
            </p>
            <div>{{ article.body }}</div>
        </div>
    </div>
{% endblock %}
  1. 增加請求所對應的url(./blog/urls.py)
urlpatterns = [
    path('', views.blog_title),
    path('<int:article_id>/', views.blog_article),
]

URL配置和查詢

當用戶通過瀏覽器請求某個URL時,Django會根據請求路徑依次在URLConf中查詢,並將第一個符合條件的對映關係作為查詢結果。

例如,訪問 http://localhost:8000/blog/1,其請求過程如下:

  1. localhost:8000:主域名部分,不進行查詢
  2. /blog/:首先在 專案目錄/urls.py 中查詢,遇到符合條件的URL對映(path('blog/', include('blog.urls')),),根據此對映中的描述,到 blog.urls(./blog/urls.py) 中查詢
  3. /1:在 ./blog/urls.py 中有URL(path('<int:article_id>/', views.blog_article))配置,請求路徑正好符合,從而確定最終訪問的檢視函式 views.blog_