天天看點

Django實戰----頁面靜态化

關于頁面靜态化

前言:

因為代碼在windows上運作,Linux上的定時任務無法使用,正常來說要把頁面靜态化的腳本做成定時任務,每過一段時間自動渲染一次,因為MySQL内的資料是會變化的

為什麼要做頁面靜态化

  1. 減少資料庫查詢次數。
  2. 提升頁面響應效率。

具體的做法

  1. 将動态渲染生成的頁面結果儲存成html檔案,放到靜态檔案伺服器中。
  2. 使用者直接去靜态伺服器,通路處理好的靜态html檔案。

哪些資料不能靜态化處理

  1. 使用者相關資料不能靜态化:

    1.1 使用者名、購物車等不能靜态化。

  2. 動态變化的資料不能靜态化:

    2.1熱銷排行、新品推薦、分頁排序資料等等。

  3. 不能靜态化的資料處理:

    3.1 可以在使用者得到頁面後,在頁面中向後端發送Ajax請求擷取相關資料。

    3.2 直接使用模闆渲染出來。

    3.3 其他合理的處理方式等等。

渲染index.html靜态化(代碼)

  1. 查詢相關的資料 組織成前端模闆需要的資料
  2. 加載需要渲染的模闆

    2.1 擷取模闆對象 loader子產品的get_template(‘模闆名字’)方法

    from django.template import loader

    index_template = loader.get_template('index.html')

  3. 資料渲染到模闆上面

    3.1 render方法(資料)

    index_html_data = index_template.render(context)

  4. 把渲染完成後的模闆放入到靜态伺服器中

代碼

import os

from apps.contents.models import ContentCategory
from meiduo_market import settings
from utils.goods import get_categories


def generate_static_index_html():
    # 1查詢相關資料
    # 擷取頻道和分類資料
    categories = get_categories()

    # 廣告資料
    contents = {}
    # 所有廣告分類
    content_categories = ContentCategory.objects.all()
    # 周遊 擷取分類下所有廣告 添加到字典
    for cat in content_categories:
        contents[cat.key] = cat.content_set.filter(status=True).order_by('sequence')

    # 渲染模闆的上下文
    context = {
        'categories': categories,
        'contents': contents,
    }

    # 2加載需要渲染模闆
    from django.template import loader
    # 擷取模闆對象
    index_template = loader.get_template('index.html')

    # 3把資料渲染到模闆上
    index_html_data = index_template.render(context)

    # 4把渲染好的模闆儲存成html檔案,寫入到指定檔案
    # settings.BASE_DIR         /home/halon/Desktop/meiduo_pro/meiduo
    # os.path.dirname(settings.BASE_DIR)      /home/halon/Desktop/meiduo_pro

    # / home / halon / Desktop / meiduo_pro/front_end_pc/index.html

    file_path = os.path.join(os.path.dirname(settings.BASE_DIR), 'front_end_pc/index.html')
    print(file_path)

    with open(file_path, 'w', encoding='utf-8') as f:
        f.write(index_html_data)
           

渲染商品詳情頁面(代碼)

  1. 加載Django環境
# 加載django環境
import os

os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'meiduo.settings')
import django

django.setup()
           
  1. 查詢出相關資料 組織成前端需要的資料格式
  2. 加載需要渲染的模闆
  3. 渲染模闆
  4. 儲存渲染後的模闆到指定位置

代碼

#!/usr/bin/env python

import sys

sys.path.insert(0, '../')

# 加載django環境
import os

os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'meiduo.settings')
import django

django.setup()

from apps.goods.models import SKU
from utils.goods import get_goods_specs, get_categories, get_breadcrumb

from django.conf import settings

from apps.goods import models


def generate_static_sku_detail_html(sku_id):
    # - 1查詢相關資料
    """提供商品詳情頁"""
    # 擷取目前sku的資訊
    sku = SKU.objects.get(id=sku_id)
    # 查詢商品頻道分類
    categories = get_categories()
    # 查詢面包屑導航
    breadcrumb = get_breadcrumb(sku.category)
    # 擷取規格
    specs = get_goods_specs(sku)

    # 渲染頁面
    context = {
        'categories': categories,
        'breadcrumb': breadcrumb,
        'sku': sku,
        'specs': specs,
    }
    # - 2加載需要渲染模闆

    from django.template import loader
    # 擷取模闆對象
    index_template = loader.get_template('detail.html')

    # 3把資料渲染到模闆上
    index_html_data = index_template.render(context)
    # 4把渲染好的模闆儲存成html檔案寫入到指定檔案
    file_path = os.path.join(os.path.dirname(settings.BASE_DIR), 'front_end_pc/goods/' + str(sku_id) + '.html')
    with open(file_path, 'w') as f:
        f.write(index_html_data)


if __name__ == '__main__':
    skus = models.SKU.objects.all()
    for sku in skus:
        print(sku.id)
        generate_static_sku_detail_html(sku.id)