【django】性能优化——页面静态化【37】

导读:本篇文章讲解 【django】性能优化——页面静态化【37】,希望对大家有帮助,欢迎收藏,转发!站点地址:www.bmabk.com

#0# 一、⻚⾯静态化介绍

1、为什么要做⻚⾯静态化
减少数据库查询次数。
提升⻚⾯响应效率。

2、什么是⻚⾯静态化
将动态渲染⽣成的⻚⾯结果保存成html⽂件,放到静态⽂件服务器中。
⽤户直接去静态服务器,访问处理好的静态html⽂件。

在这里插入图片描述

二、⾸⻚⻚⾯静态化实现

⾸⻚⻚⾯静态化实现步骤

1、在newsapp下创建static_index.py⽂件
在这里插入图片描述

import os

from django.conf import settings
from django.shortcuts import render
from django.template import loader
from django.utils import timezone
from django.views.generic.base import View
# Create your views here.
from .models import NewsChannel, Article
from django import http
from django.core.paginator import Paginator, EmptyPage


def static_index_html():
    '''
    将新闻首页实现页面静态化处理
    :return:
    '''
    #准备base.html需要的数据
    channels=NewsChannel.objects.order_by('id')

    newschannel = NewsChannel.objects.get(id=1)


    # 获取当前频道下的所有类别id
    category_id_list = [category.id for category in newschannel.newscategory_set.all() if category]
    # 查询当前频道下的所有文章
    articles = Article.objects.filter(category_id__in=category_id_list).order_by('id')

    # 创建分页器对象
    page_obj = Paginator(articles, 3)
    # 获取当前页数据

    page_articles = page_obj.page(1)

    #准备页面数据
    context={
        'articles': page_articles,
        'channel_id': 1,
        'channels':channels,
    }

    # 获取首页模板文件
    template = loader.get_template('newsapp/index.html')
    # 渲染首页html字符串
    html_text = template.render(context)
    # 将首页html字符串写入到指定目录,命名'index.html'
    file_path = os.path.join(settings.STATICFILES_DIRS[0], 'index.html')
    with open(file_path, 'w', encoding='utf-8') as f:
        f.write(html_text)

    print(timezone.localtime().strftime('%H:%M:%S'))

2、编辑 base.html,将不需要静态化的内容去掉

	{% if user %}
        {% if user.username %}
            欢迎您! <a href="/usercenter/"><span style="font-weight: bolder;">[ {{ user.username }} ]</span></a>
            &emsp;<a href="/logout/">退出登录</a>&nbsp;
            {% else %}
            <li>
                <a href="/login/">登录</a>&nbsp;&nbsp;&nbsp;
                <a href="/register/">注册</a>
            </li>
        {% endif %}
    {% endif %}

2、⾸⻚⻚⾯静态化测试效果

在这里插入图片描述

在这里插入图片描述

使⽤Python⾃带的http.server模块来模拟静态服务器,提供静态⾸⻚的访问测试。

$ cd mgproject/mgproject
$ python -m http.server 8000 --bind 127.0.0.1

# 启动完静态服务器后浏览器输⼊地址测试
http://127.0.0.1:8000/static/index.html

在这里插入图片描述
在这里插入图片描述

三、定时任务crontab静态化⾸⻚

在Django执⾏定时任务,可以通过django-crontab扩展来实现。

1、安装 django-crontab

$ pip install django-crontab

2.注册 django-crontab 应⽤

INSTALLED_APPS = [  
'django_crontab', # 定时任务
	]

3.配置文件中设置定时任务

在这里插入图片描述

#定时任务相关配置
CRONJOBS = [
    # 每1分钟⽣成⼀次⾸⻚静态⽂件
    ('*/1 * * * *', 'newsapp.static_index.static_index_html', '>' + os.path.join(BASE_DIR, 'logs/crontab.log'))]# 解决 crontab 中⽂问题CRONTAB_COMMAND_PREFIX = 'LANG_ALL=zh_cn.UTF-8'

4、管理定时任务

# 添加定时任务到系统中
$ python manage.py crontab add
# 显示已激活的定时任务
$ python manage.py crontab show
# 移除定时任务
$ python manage.py crontab remove

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

文章由极客之音整理,本文链接:https://www.bmabk.com/index.php/post/74061.html

(0)
小半的头像小半

相关推荐

极客之音——专业性很强的中文编程技术网站,欢迎收藏到浏览器,订阅我们!