django-配置静态页面-celery/redis/nginx
celery生成静态页面
celery_tasks/tasks.py
# 生成静态首页
from django.template import loader, RequestContext # templates包
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') # 需要导入templates包
# 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_index.html
{% extends 'layout/static_base.html' %}
{% load staticfiles %}
{% block title %}天天生鲜-首页{% endblock title %}
{% block topfiles %}
<script type="text/javascript" src="{% static 'js/jquery-1.12.4.min.js' %}"></script>
<script type="text/javascript" src="{% static 'js/jquery-ui.min.js' %}"></script>
<script type="text/javascript" src="{% static 'js/slide.js' %}"></script>
{% endblock topfiles %}
{% block body %}
<div class="navbar_con">
<div class="navbar">
<h1 class="fl">全部商品分类</h1>
<ul class="navlist fl">
<li><a href="">首页</a></li>
<li class="interval">|</li>
<li><a href="">手机生鲜</a></li>
<li class="interval">|</li>
<li><a href="">抽奖</a></li>
</ul>
</div>
</div>
<div class="center_con clearfix">
{# 导航菜单 #}
<ul class="subnav fl">
{% for type in types %}
<li><a href="#model0{{ forloop.counter }}" class="{{ type.logo }}">{{ type.name }}</a></li>
{% endfor %}
</ul>
{# 轮播图 #}
<div class="slide fl">
<ul class="slide_pics">
{% for banner in goods_banners %}
<li><a href="{% url 'goods:detail' banner.sku.id %}"><img src="{{ banner.image.url }}" alt="幻灯片"></a></li>
{% endfor %}
</ul>
<div class="prev"></div>
<div class="next"></div>
<ul class="points"></ul>
</div>
{# 商品活动 #}
<div class="adv fl">
{% for banner in promotion_banners %}
<a href="{{ banner.url }}"><img src="{{ banner.image.url }}"></a>
{% endfor %}
</div>
</div>
{% for type in types %}
{# 首页分类商品展示信息 #}
<div class="list_model">
<div class="list_title clearfix">
<h3 class="fl" id="model0{{ forloop.counter }}">{{ type.name }}</h3>
{# 分类商品的文字展示信息 #}
<div class="subtitle fl">
<span>|</span>
{% for banner in type.title_banners %}
<a href="{% url 'goods:detail' banner.sku.id %}">{{ banner.sku.name }}</a>
{% endfor %}
</div>
<a href="#" class="goods_more fr" id="fruit_more">查看更多 ></a>
</div>
<div class="goods_con clearfix">
<div class="goods_banner fl"><img src="{{ type.image.url }}"></div>
{# 分类商品的图片展示信息 #}
<ul class="goods_list fl">
{% for banner in type.image_banners %}
<li>
<h4><a href="{% url 'goods:detail' banner.sku.id %}">{{ banner.sku.name }}</a></h4>
<a href="{% url 'goods:detail' banner.sku.id %}"><img src="{{ banner.sku.image.url }}"></a>
<div class="prize">¥ {{ banner.sku.price }}</div>
</li>
{% endfor %}
</ul>
</div>
</div>
{% endfor %}
{% endblock body %}
启动celery
celery -A celery_tasks.tasks worker -l info
生成静态页面
>>> from celery_tasks.tasks import generate_static_index_html
>>> res = generate_static_index_html.delay()
>>> res
<AsyncResult: 917ef40d-2c98-43d8-ac4f-0cb6d81d1358>
配置nginx.conf
sudo vi /usr/local/nginx/conf/nginx.conf中在http中添加
server {
listen 80;
server_name localhost;
#charset koi8-r;
#access_log logs/host.access.log main;
location /static {
# 指定静态文件存放的目录
alias /home/python/bj18/ttsx/dailyfresh/static/;
}
location / {
# 指定首页静态文件存放的目录
root /home/python/bj18/ttsx/dailyfresh/static/;
index index.html index.htm;
}
重启nginx
sudo sbin/nginx -s reload
浏览器中 直接输入绑定的ip地址就可进入静态主页

数据更新或删除时自动生成静态页面
文档:https://yiyibooks.cn/xx/django_182/ref/contrib/admin/index.html
goods/admin.py管理更新首页表数据重新生成静态首页
from django.contrib import admin
from goods.models import * # Register your models here. class BaseModelAdmin(admin.ModelAdmin):
def save_model(self, request, obj, form, change):
'''新增或修改的时候调用'''
# 调用父类的save_model()方法
super().save_model(request, obj, form, change)
# 调用celery重新生成
from celery_tasks.tasks import generate_static_index_html # 不能放在上面 不然会报错
generate_static_index_html.delay()
# 清除首页缓存数据 def delete_model(self, request, obj):
'''删除数据时候调用'''
# 调用父类delete_model()方法
super().delete_model(request, obj)
# 重新生成静态页面
from celery_tasks.tasks import generate_static_index_html # 不能放在上面 不然会报错
generate_static_index_html.delay()
# 清除首页缓存数据 class IndexPromotionAdmin(BaseModelAdmin):
pass class IndexTypeGoodsBannerAdmin(BaseModelAdmin):
pass class GoodsTypeAdmin(BaseModelAdmin):
pass class IndexGoodsBannerAdmin(BaseModelAdmin):
pass admin.site.register(IndexPromotionBanner, IndexPromotionAdmin)
admin.site.register(IndexGoodsBanner, IndexGoodsBannerAdmin)
admin.site.register(GoodsType, GoodsTypeAdmin)
admin.site.register(IndexTypeGoodsBanner, IndexTypeGoodsBannerAdmin)
使用缓存
文档:https://yiyibooks.cn/xx/django_182/topics/cache.html
底层缓存API
视图函数views.py中导入cache包
from django.core.cache import cache # 底层缓存API
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) return render(request, 'index.html', context)
清除缓存
站点管理goods/admin.py
from django.core.cache import cache
class BaseModelAdmin(admin.ModelAdmin):
def save_model(self, request, obj, form, change):
'''新增或修改的时候调用'''
# 调用父类的save_model()方法
super().save_model(request, obj, form, change)
# 调用celery重新生成
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):
'''删除数据时候调用'''
# 调用父类delete_model()方法
super().delete_model(request, obj)
# 重新生成静态页面
from celery_tasks.tasks import generate_static_index_html # 不能放在上面 不然会报错
generate_static_index_html.delay()
# 清除首页缓存数据
cache.delete('index_page_data')
django-配置静态页面-celery/redis/nginx的更多相关文章
- django配置静态文件
django配置静态文件 参考文章链接:http://blog.csdn.net/hireboy/article/details/8806098
- django配置静态文件的两种方法
方法一:按照django配置静态文件的方法,可以在APP应用目录下创建一个static的文件夹,然后在static文件夹下创建一个和APP同名的文件夹,如我有一个blog的django项目,在下面有一 ...
- Beego没gin配置静态页面方便
上代码 腾讯这个例子还是很值得学习的,不轻有东西,单也不重到看着都蒙圈的样子. https://github.com/Tencent/bk-cmdb/blob/master/src/web_serve ...
- nginx配置静态页面访问
server { server_name static.naice.me; // 你的域名或者 ip # 域名:static.naice.me 这里用 "_" 代表获取匹配所有 ...
- Django配置静态文件(CSS\js)及Django调用JS、CSS、图片等静态文件
1 新建一项目: root@python:django-admin.py startproject csstest root@python:cd csstest root@python:ls csst ...
- Django配置404页面
一.settings配置 1.首先需要在settings中将DEBUG由原来的True改为False DEBUG = False 2.需要设置 ALLOWED_OSTS = ["*" ...
- Django1.11.7配置静态文件
Django配置静态文件分为三步 1.建文件夹 2.设置setting 3.页面引用 1.文件目录结构 在APP下新建static文件夹,将js和css文件放入文件夹 2.配置settings.py ...
- 配置静态服务器和配置nfs
一.配置Nginx 1.安装Nginx yum -y install nginx 2.编写配置文件 [root@ngix nginx]# cd /etc/nginx [root@ngix nginx] ...
- Django之使用celery和NGINX生成静态页面实现性能优化
性能优化原理: 当我们要给client浏览器返回一个页面时,我们需要去数据库查询数据并将数据和基本页面模板渲染形成页面返回给客户端,但如果每一个用户访问时都去查询一次首页的的数据时,当日访问量很大时那 ...
随机推荐
- vue判断图片为空或者图片加载不成功时显示默认图片
纯css解决方案: <img src="broken.png" alt=""> img { position: relative; } img:af ...
- windows和linux环境下java调用C++代码-JNI技术
最近部门做安卓移动开发的需要调C++的代码,困难重重,最后任务交给了我,查找相关资料,没有一个教程能把不同环境(windows,linux)下怎么调用说明白的,自己在实现的过程中踩了几个坑,在这里总结 ...
- PLC采集与控制,实现MES工序管理与品质管控,记录产品的加工数据,工厂生产装配流水线的一次成功应用
1.通过程序与PLC的采集与控制,实现MES工序管理,品质管控,历史数据追溯的目的 2.大概的流程图 3.有三个地方相关联来实现以上功能,首先是MES的工序管理,设置指定的产品有那些工序,上位机程序扫 ...
- Html 对象的常用事件列举
事件名称 触发时间 对象例举 OnBlur 对象失去输入焦点 窗口和所有的表单对象 OnChange 用户改变对象的值 文本框.文本区域.选择列表等 OnClick 用户鼠标点击 链接.按钮.单选钮. ...
- (十二)一个简单的pdf文件体
%PDF-1.0 % 文件头,说明符合PDF1.0规范 1 0 obj %对象号 产生号(修改次数) ...
- 在ASP.NET MVC中加载部分视图的方法及差别
在视图里有多种方法可以加载部分视图,包括Partial() .Action().RenderPartial().RenderAction().RenderPage()方法.下面说明一下这些方法的差别. ...
- Nas 系统的虚拟化方案
Nas 系统的虚拟化方案 https://zhuanlan.zhihu.com/p/55025102 对搞技术的人来说,Nas 是个理想的玩具,既然是程序员用的 Nas ,自然要专业一点,不能像小白一 ...
- Mybatis中使用association及collection进行自关联示例(含XML版与注解版)
XML版本: 实体类: @Data @ToString @NoArgsConstructor public class Dept { private Integer id; private Strin ...
- JS国际化网站中英文切换(理论支持所有语言)应用于h5版APP
网页框架类APP实现国际化参考文案一 参考:https://blog.csdn.net/CSDN_LQR/article/details/78026254 另外付有自己实现的方法 本人用于H5版的AP ...
- 【转载】C#中float.TryParse方法和float.Parse方法的异同之处
在C#编程过程中,float.TryParse方法和float.Parse方法都可以将字符串string转换为单精度浮点类型float,但两者还是有区别,最重要的区别在于float.TryParse方 ...