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的更多相关文章

  1. django配置静态文件

    django配置静态文件 参考文章链接:http://blog.csdn.net/hireboy/article/details/8806098

  2. django配置静态文件的两种方法

    方法一:按照django配置静态文件的方法,可以在APP应用目录下创建一个static的文件夹,然后在static文件夹下创建一个和APP同名的文件夹,如我有一个blog的django项目,在下面有一 ...

  3. Beego没gin配置静态页面方便

    上代码 腾讯这个例子还是很值得学习的,不轻有东西,单也不重到看着都蒙圈的样子. https://github.com/Tencent/bk-cmdb/blob/master/src/web_serve ...

  4. nginx配置静态页面访问

      server { server_name static.naice.me; // 你的域名或者 ip # 域名:static.naice.me 这里用 "_" 代表获取匹配所有 ...

  5. Django配置静态文件(CSS\js)及Django调用JS、CSS、图片等静态文件

    1 新建一项目: root@python:django-admin.py startproject csstest root@python:cd csstest root@python:ls csst ...

  6. Django配置404页面

    一.settings配置 1.首先需要在settings中将DEBUG由原来的True改为False DEBUG = False 2.需要设置 ALLOWED_OSTS = ["*" ...

  7. Django1.11.7配置静态文件

    Django配置静态文件分为三步 1.建文件夹 2.设置setting 3.页面引用 1.文件目录结构 在APP下新建static文件夹,将js和css文件放入文件夹 2.配置settings.py ...

  8. 配置静态服务器和配置nfs

    一.配置Nginx 1.安装Nginx yum -y install nginx 2.编写配置文件 [root@ngix nginx]# cd /etc/nginx [root@ngix nginx] ...

  9. Django之使用celery和NGINX生成静态页面实现性能优化

    性能优化原理: 当我们要给client浏览器返回一个页面时,我们需要去数据库查询数据并将数据和基本页面模板渲染形成页面返回给客户端,但如果每一个用户访问时都去查询一次首页的的数据时,当日访问量很大时那 ...

随机推荐

  1. 更改ubuntu桌面环境

    1.修改桌面环境 update-alternatives --config x-session-manager 2.卸载某一种ppa源下面的软件 sudo apt-get install ppa-pu ...

  2. Java集合框架(Java总结五)

    ”https://www.runoob.com/java/” 集合接口区别 List 接口存储一组不唯一,有序(插入顺序)的对象,允许有相同的元素. Set 接口存储一组唯一,无序的对象,不保存重复的 ...

  3. linux安装 uwsgi 测试 test.py 不显示hello world 的解决办法

    一般部署项目到服务器,会安装uwsgi,但是很多教程在安装它的时候会让你测试一下安装好了没,于是就有很多像我一样懵逼的少年掉进一个坑里出不来,很久.很久... 那就是最后浏览器输入ip:8000端口后 ...

  4. C语言conio.h部分解释

    #include <conio.h> int getch(void);// 从控制台得到下一个字符,以ASCII值返回,并不在屏幕显示该字符 int getche(void);// 从控制 ...

  5. 关键字ref、out

    通常,变量作为参数进行传递时,不论在方法内进行了什么操作,其原始初始化的值都不会被影响: 例如: public void TestFun1() { ; TestFun2(arg); Console.W ...

  6. 自学Python编程的第十天(希望有IT大牛看见的指点小弟我,万分感谢)---------来自苦逼的转行人

    2019-09-20-23:24:15 今天逛论坛.逛知识星球时.逛b站up主时,都说到低学历,非科班的人最好不要去自学Python 他们都说:如果我们学python是为了找工作,最好不要把pytho ...

  7. git恢复已删的分支

    git恢复已经删除的分支 执行git命令, 找回之前提交的commit git log -g 执行效果 commit 80fd3a3e1abeab52030ee9f6ec32b5c815de20a9 ...

  8. 【面试突击】- 2019年125条常见的java面试笔试题汇总(二)

    26.什么时候用assert. assertion(断言)在软件开发中是一种常用的调试方式,很多开发语言中都支持这种机制.在实现中,assertion就是在程序中的一条语句,它对一个boolean表达 ...

  9. 【面试突击】-RabbitMQ常见面试题(二)

    问题一:RabbitMQ 中的 broker 是指什么?cluster 又是指什么? 答:broker 是指一个或多个 erlang node 的逻辑分组,且 node 上运行着 RabbitMQ 应 ...

  10. 手写MVC框架(二)-代码实现和使用示例

    --------上一篇:手写MVC框架(一)-再出发----- 背景 书接上文,之前整理了实现MVC框架需要写哪些东西.这周粗看了一下,感觉也没多少工作量,所以就计划一天时间来完成.周末的时间,哪会那 ...