6、Django 第三方工具

一、富文本编辑器

其它富文本编辑器:https://blog.csdn.net/qq_38200194/article/details/100129740

  • 借助富文本编辑器,管理员能够编辑出来一个包含html的页面,从而页面的显示效果,可以由管理员定义,而不用完全依赖于前期开发人员
  • 此处以tinymce为例,其他富文本编辑器的使用可以自行学习
  • 使用编辑器的显示效果为:

1、下载安装

在网站https://pypi.org/网站搜索并下载“ django-tinymce-2.4.0” 

# 解压
tar zxvf django-tinymce-2.4.0.tar.gz

# 进入目录
python setup.py install

2、应用到项目中。

  • 在settings.py中为INSTALLED_APPS添加编辑器应用
INSTALLED_APPS = (
    ...
    'tinymce',
)
  • 在settings.py中添加编辑配置项
TINYMCE_DEFAULT_CONFIG = {
    'theme': 'advanced',
    'width': 600,
    'height': 400,
}
  • 在根urls.py中配置
urlpatterns = [
    ...
    url(r'^tinymce/', include('tinymce.urls')),
]
  • 在应用中定义模型的属性
from django.db import models
from tinymce.models import HTMLField

class HeroInfo(models.Model):
    ...
    hcontent = HTMLField()

在后台管理界面中,就会显示为富文本编辑器,而不是多行文本框

2、自定义使用。

  • 定义视图编辑器,用于显示编辑器并完成提交
def editor(request):
    return render(request, 'other/editor.html')
  • 配置网址
urlpatterns = [
    ...
    url(r'^editor/$', views.editor, name='editor'),
]
  • 创建模板editor.html
<!DOCTYPE html>
<html>
<head>
    <title></title>
    <script type="text/javascript" src='/static/tiny_mce/tiny_mce.js'></script>
    <script type="text/javascript">
        tinyMCE.init({
            'mode':'textareas',
            'theme':'advanced',
            'width':400,
            'height':100
        });
    </script>
</head>
<body>
<form method="post" action="/content/">
    <input type="text" name="hname">
    <br>
    <textarea name='hcontent'>哈哈,这是啥呀</textarea>
    <br>
    <input type="submit" value="提交">
</form>
</body>
</html>
  • 定义视图内容,接收请求,并更新heroinfo对象
def content(request):
    hname = request.POST['hname']
    hcontent = request.POST['hcontent']

    heroinfo = HeroInfo.objects.get(pk=1)
    heroinfo.hname = hname
    heroinfo.hcontent = hcontent
    heroinfo.save()

    return render(request, 'other/content.html', {'hero': heroinfo})
  • 添加url项
urlpatterns = [
    ...
    url(r'^content/$', views.content, name='content'),
]
  • 定义模板content.html
<!DOCTYPE html>
<html>
<head>
    <title></title>
</head>
<body>
姓名:{{hero.hname}}
<hr>
{%autoescape off%}
{{hero.hcontent}}
{%endautoescape%}
</body>
</html>

二、缓存

  • 对于中等流量的网站来说,尽可能地减少开销是必要的。缓存数据就是为了保存那些需要很多计算资源的结果,这样的话就不必在下次重复消耗计算资源
  • Django自带了一个健壮的缓存系统来保存动态页面,避免对于每次请求都重新计算
  • Django提供了不同级别的缓存粒度:可以缓存特定视图的输出、可以仅仅缓存那些很难生产出来的部分、或者可以缓存整个网站

1、设置缓存

  • 通过设置决定把数据缓存在哪里,是数据库中、文件系统还是在内存中
  • 通过setting文件的CACHES配置来实现
  • 参数TIMEOUT:缓存的默认过期时间,以秒为单位,这个参数默认是300秒,即5分钟;设置TIMEOUT为None表示永远不会过期,值设置成0造成缓存立即失效
CACHES={
    'default': {
        'BACKEND': 'django.core.cache.backends.locmem.LocMemCache',
        'TIMEOUT': 60,
    }
}

可以将cache存到redis中,默认采用1数据库,需要安装包并配置如下:

安装包:pip install django-redis-cache

CACHES = {
    "default": {
        "BACKEND": "redis_cache.cache.RedisCache",
        "LOCATION": "localhost:6379",
        'TIMEOUT': 60,
    },
}

可以连接redis查看存的数据

连接:redis-cli
切换数据库:select 1
查看键:keys *
查看值:get 键

2、单个view缓存

  • django.views.decorators.cache定义了cache_page装饰器,用于对视图的输出进行缓存
  • 示例代码如下:
from django.views.decorators.cache import cache_page

@cache_page(60 * 15)
def index(request):
    return HttpResponse('hello1')
    #return HttpResponse('hello2')
  • cache_page接受一个参数:timeout,秒为单位,上例中缓存了15分钟
  • 视图缓存与URL无关,如果多个URL指向同一视图,每个URL将会分别缓存

3、模板片断缓存

  • 使用cache模板标签来缓存模板的一个片段
  • 需要两个参数:
    • 缓存时间,以秒为单位
    • 给缓存片段起的名称
  • 示例代码如下:
{% load cache %}
{% cache 500 hello %}
hello1
<!--hello2-->
{% endcache %}

4、底层的缓存API

from django.core.cache import cache

设置:cache.set(键,值,有效时间)
获取:cache.get(键)
删除:cache.delete(键)
清空:cache.clear()

三、全文检索

四、celery

五、部署

原文地址:https://www.cnblogs.com/sunshine-long/p/12507723.html