栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

Django电商项目(五)页面静态化、设置缓存、首页内容获取展示、历史记录

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Django电商项目(五)页面静态化、设置缓存、首页内容获取展示、历史记录

Django电商项目
  • 网页优化-首页页面静态化
    • 配置nginx提交静态页面
    • admin管理更新首页数据表数据时重新生成index静态页面
  • 网页优化-页面缓存
    • 更新缓存
  • 首页内容获取和展示
  • 商品历史记录

网页优化-首页页面静态化

首页访问次数多 不经常更改可以进行页面静态化

把原本动态的页面处理结果保存成html文件,让用户直接访问这个生成出来的静态的html页面

在celery_tasks.tasks.py再定义一个任务函数 使用首页的view函数进行修改

from django.template import loader, RequestContext
from goods.models import GoodsType,IndexGoodsBanner,IndexPromotionBanner,IndexTypeGoodsBanner
from django_redis import get_redis_connection

@app.task
def generate_static_index_html():
    '''产生首页静态页面'''
    # 获取商品的种类信息
    types = GoodsType.objects.all()

    # 获取首页轮播商品信息
    goods_banners = IndexGoodsBanner.objects.all().order_by('index')

    # 获取首页促销活动信息
    promotion_banners = IndexPromotionBanner.objects.all().order_by('index')

    # 获取首页分类商品展示信息
    for type in types:  # GoodsType
        # 获取type种类首页分类商品的图片展示信息
        image_banners = IndexTypeGoodsBanner.objects.filter(type=type, display_type=1).order_by('index')
        # 获取type种类首页分类商品的文字展示信息
        title_banners = IndexTypeGoodsBanner.objects.filter(type=type, display_type=0).order_by('index')

        # 动态给type增加属性,分别保存首页分类商品的图片展示信息和文字展示信息
        type.image_banners = image_banners
        type.title_banners = title_banners


    # 组织模板上下文
    context = {'types': types,
               'goods_banners': goods_banners,
               'promotion_banners': promotion_banners}

    # 使用模板
    # 1.加载模板文件,返回模板对象
    temp = loader.get_template('static_index.html')
    # 2.模板渲染
    static_index_html = temp.render(context)

    # 生成首页对应静态文件
    save_path = os.path.join(settings.base_DIR, 'static/index.html')
    with open(save_path, 'w') as f:
        f.write(static_index_html)

新建模板文件static_base.html去除登录后相关内容

并新建static_index.html使其继承static_base.html 内容与index文件一样
仍需将项目文件放置在celery所在服务器中并进行重新启动celery
celery -A celery_tasks.tasks worker -l info

在djanggo执行

可以在celery所在服务器生成静态文件

配置nginx提交静态页面

sudo vi ./conf/nginx.conf
新添加一个server端口不与之前的重复

添加保存后重启nginx即可

admin管理更新首页数据表数据时重新生成index静态页面

参考文档https://doc.codingdict.com/django/ref/contrib/admin/index.html#modeladmin-methods

goods/admin.py

from django.contrib import admin
from django.core.cache import cache
from goods.models import GoodsType,IndexPromotionBanner,IndexGoodsBanner,IndexTypeGoodsBanner
# Register your models here.


class baseModelAdmin(admin.ModelAdmin):
    def save_model(self, request, obj, form, change):
        '''新增或更新表中的数据时调用'''
        super().save_model(request, obj, form, change)

        # 发出任务,让celery worker重新生成首页静态页
        from celery_tasks.tasks import generate_static_index_html
        generate_static_index_html.delay()

        # 清除首页的缓存数据
        cache.delete('index_page_data')

    def delete_model(self, request, obj):
        '''删除表中的数据时调用'''
        super().delete_model(request, obj)
        # 发出任务,让celery worker重新生成首页静态页
        from celery_tasks.tasks import generate_static_index_html
        generate_static_index_html.delay()

        # 清除首页的缓存数据
        cache.delete('index_page_data')


class GoodsTypeAdmin(baseModelAdmin):
    pass


class IndexGoodsBannerAdmin(baseModelAdmin):
    pass


class IndexTypeGoodsBannerAdmin(baseModelAdmin):
    pass


class IndexPromotionBannerAdmin(baseModelAdmin):
    pass


admin.site.register(GoodsType, GoodsTypeAdmin)
admin.site.register(IndexGoodsBanner, IndexGoodsBannerAdmin)
admin.site.register(IndexTypeGoodsBanner, IndexTypeGoodsBannerAdmin)
admin.site.register(IndexPromotionBanner, IndexPromotionBannerAdmin)

修改或删除会重新生成静态页面


修改首页url为/index

网页优化-页面缓存

将处理计算的结果先临时保存起来,下次使用的时候可以先直接使用,如果没有这个备份的数据,重新进行计算处理

将缓存数据保存在内存中 (本项目中保存在redis中,获取速度快)
cache
修改了数据库的数据,直接删除缓存
缓存要设置有效期

django文档:
https://doc.codingdict.com/django/topics/cache.html
站点级别缓存,将整个网站进行缓存,占用空间大
单个view缓存,页面在不同时间可能会有区别 不太合适
模板片段缓存,最后使用模板 不太合适
使用底层API缓存

from django.core.cache import cache

# http://127.0.0.1:8000
class IndexView(View):
    '''首页'''
    def get(self, request):
        '''显示首页'''
        # 尝试从缓存中获取数据
        context = cache.get('index_page_data')

        if context is None:
            print('设置缓存')
            # 缓存中没有数据
            # 获取商品的种类信息
            types = GoodsType.objects.all()

            # 获取首页轮播商品信息
            goods_banners = IndexGoodsBanner.objects.all().order_by('index')

            # 获取首页促销活动信息
            promotion_banners = IndexPromotionBanner.objects.all().order_by('index')

            # 获取首页分类商品展示信息
            for type in types: # GoodsType
                # 获取type种类首页分类商品的图片展示信息
                image_banners = IndexTypeGoodsBanner.objects.filter(type=type, display_type=1).order_by('index')
                # 获取type种类首页分类商品的文字展示信息
                title_banners = IndexTypeGoodsBanner.objects.filter(type=type, display_type=0).order_by('index')

                # 动态给type增加属性,分别保存首页分类商品的图片展示信息和文字展示信息
                type.image_banners = image_banners
                type.title_banners = title_banners

            context = {'types': types,
                       'goods_banners': goods_banners,
                       'promotion_banners': promotion_banners}
            # 以上内容每个用户都一样 可以设置缓存
            # key  value timeout
            cache.set('index_page_data', context, 3600)

        # 获取用户购物车中商品的数目
        user = request.user
        cart_count = 0
        if user.is_authenticated():
            # 用户已登录
            conn = get_redis_connection('default')
            cart_key = 'cart_%d'%user.id
            cart_count = conn.hlen(cart_key)

        # 组织模板上下文
        context.update(cart_count=cart_count)

        # 使用模板
        return render(request, 'index.html', context)


更新缓存


网页优化的优点

DDOS:黑客控制大批量肉鸡访问网站

首页内容获取和展示

购物车参数显示

修改模板文件

{% extends 'base.html' %}
{% load staticfiles %}
{% block title %}天天生鲜-首页{% endblock title %}
{% block topfiles %}
	
	
	
{% endblock topfiles %}
{% block body %}
	

	
    {% for banner in goods_banners %}
  • 幻灯片
  • {% endfor %}
    {% for banner in promotion_banners %} {% endfor %}
    {% for type in types %}

    {{ type.name }}

    | {% for banner in type.title_banners %} {{ banner.sku.name }} {% endfor %}
    查看更多 >
    {% endfor %} {% endblock body %}

    创建视图函数
    goods/view.py

    # http://127.0.0.1:8000
    class IndexView(View):
        '''首页'''
        def get(self, request):
            '''显示首页'''
            # 尝试从缓存中获取数据
            context = cache.get('index_page_data')
    
            if context is None:
                print('设置缓存')
                # 缓存中没有数据
                # 获取商品的种类信息
                types = GoodsType.objects.all()
    
                # 获取首页轮播商品信息
                goods_banners = IndexGoodsBanner.objects.all().order_by('index')
    
                # 获取首页促销活动信息
                promotion_banners = IndexPromotionBanner.objects.all().order_by('index')
    
                # 获取首页分类商品展示信息
                for type in types: # GoodsType
                    # 获取type种类首页分类商品的图片展示信息
                    image_banners = IndexTypeGoodsBanner.objects.filter(type=type, display_type=1).order_by('index')
                    # 获取type种类首页分类商品的文字展示信息
                    title_banners = IndexTypeGoodsBanner.objects.filter(type=type, display_type=0).order_by('index')
    
                    # 动态给type增加属性,分别保存首页分类商品的图片展示信息和文字展示信息
                    type.image_banners = image_banners
                    type.title_banners = title_banners
    
                context = {'types': types,
                           'goods_banners': goods_banners,
                           'promotion_banners': promotion_banners}
                # 以上内容每个用户都一样 可以设置缓存
                # key  value timeout
                cache.set('index_page_data', context, 3600)
    
            # 获取用户购物车中商品的数目
            user = request.user
            cart_count = 0
            if user.is_authenticated():
                # 用户已登录
                conn = get_redis_connection('default')
                cart_key = 'cart_%d'%user.id
                cart_count = conn.hlen(cart_key)
    
            # 组织模板上下文
            context.update(cart_count=cart_count)
    
            # 使用模板
            return render(request, 'index.html', context)
    
    商品历史记录

    参考命令:redisdoc.com
    修改模板文件

    {% extends 'base_user_center.html' %}
    {% block right_content %}
    		

    基本信息

    最近浏览

      {% for goods in goods_li %}
    • {{ goods.name }}

      ¥{{ goods.price }} {{ goods.price }}/{{ goods.unite }}
    • {% empty %} 无历史浏览记录 {% endfor %}
    {% endblock right_content %}

    view.py

    # /goods/商品id
    class DetailView(View):
        '''详情页'''
        def get(self, request, goods_id):
            '''显示详情页'''
            try:
                sku = GoodsSKU.objects.get(id=goods_id)
            except GoodsSKU.DoesNotExist:
                # 商品不存在
                return redirect(reverse('goods:index'))
    
            # 获取商品的分类信息
            types = GoodsType.objects.all()
    
            # 获取商品的评论信息
            sku_orders = OrderGoods.objects.filter(sku=sku).exclude(comment='')
    
            # 获取新品信息
            new_skus = GoodsSKU.objects.filter(type=sku.type).order_by('-create_time')[:2]
    
            # 获取同一个SPU的其他规格商品
            same_spu_skus = GoodsSKU.objects.filter(goods=sku.goods).exclude(id=goods_id)
    
            # 获取用户购物车中商品的数目
            user = request.user
            cart_count = 0
            if user.is_authenticated():
                # 用户已登录
                conn = get_redis_connection('default')
                cart_key = 'cart_%d' % user.id
                cart_count = conn.hlen(cart_key)
    
                # 添加用户的历史记录
                conn = get_redis_connection('default')
                history_key = 'history_%d'%user.id
                # 移除列表中的goods_id
                conn.lrem(history_key, 0, goods_id)
                # 把goods_id插入到列表的左侧
                conn.lpush(history_key, goods_id)
                # 只保存用户最新浏览的5条信息
                conn.ltrim(history_key, 0, 4)
    
            # 组织模板上下文
            context = {'sku':sku, 'types':types,
                       'sku_orders':sku_orders,
                       'new_skus':new_skus,
                       'same_spu_skus':same_spu_skus,
                       'cart_count':cart_count}
    
            # 使用模板
            return render(request, 'detail.html', context)
    
    
    转载请注明:文章转载自 www.mshxw.com
    本文地址:https://www.mshxw.com/it/313466.html
    我们一直用心在做
    关于我们 文章归档 网站地图 联系我们

    版权所有 (c)2021-2022 MSHXW.COM

    ICP备案号:晋ICP备2021003244-6号