Django-第三方(富文本/缓存)

————富文本————
1.Rich Text Format(RTF)
①微软开发的跨平台文档格式,大多数的文字处理软件都能读取和保存RTF文档,其实就是可以添加样式的文档,和HTML有很多相似的地方
图例:

2.tinymce插件
①安装插件:pip install django-tinymce
②配置插件
③使用:
——>后台管理中:HTMLField
——>页面中使用:textarea
3.在后台管理中使用:
①配置settings.py文件
——>INSTALLED_APPS 添加 tinymce 应用
——>添加默认配置:
TINYMCE_DEFAULT_CONFIG = {
'theme':'advanced',
'width':800,
'height':600,
}
②创建模型类
from tinymce.models import HTMLField
class Blog(models.Model):
     sBlog = HTMLField()
③注册模型
admin.site.register

4.在普通页面使用
①使用文本域盛放内容
<form method='post' action='url'>
<textarea></textarea>
</form>
②添加脚本
<script src='/static/tiny_mce/tiny_mce.js'></script>
<script>
        tinyMCE.init({
            'mode': 'textareas',
            'theme': 'simple',
            'theme': 'advanced',
            'width': 800,
            'height': 600,
            'language': 'zh',
            'style_formats': [
                {'title': 'Bold text', 'inline': 'b'},
                {'title': 'Red text', 'inline': 'span', 'styles': {'color': '#ff0000'}},
                {'title': 'Red header', 'block': 'h1', 'styles': {'color': '#ff0000'}},
                {'title': 'Example 1', 'inline': 'span', 'classes': 'example1'},
                {'title': 'Example 2', 'inline': 'span', 'classes': 'example2'},
                {'title': 'Table styles'},
                {'title': 'Table row 1', 'selector': 'tr', 'classes': 'tablerow1'}
            ],
        })
</script>

5.本质上还是使用html的样式

————缓存————
1.缓存的必要性
①优化数据结构
②优化了对数据的查询,筛选,过滤
③减少了对磁盘的IO
2.缓存原理
①获取数据的数据的时候就是去缓存中拿
②拿到了直接返回
③没拿到就去数据库中查询,筛选,然后缓存到数据库, 然后返回给模板
④实现方案:内存、文件、数据库

3.Django内置的缓存模块
①缓存配置(settings.py):
——》
CACHES = {
    'default': {
        # 'BACKEND': 'django.core.cache.backends.db.DatabaseCache',
        # 'LOCATION': 'my_cache_table',
        "BACKEND": "django_redis.cache.RedisCache",
        "LOCATION": "redis://127.0.0.1:6379/1",
        "OPTIONS": {
            "CLIENT_CLASS": "django_redis.client.DefaultClient",
        }
    }
}
——》 python manage.py createcachetable [xxx]
②@cache_page(60):
——> timeout
——> cache #缓存到哪一个库中/很少使用/针对于系统配置了多个缓存
——> key_prefix #前缀
——> 举栗子: @cache_page(60,cache='sqlite',key_prefix='sl-')
③原生cache:
set(key, value, timeout)
get(key)
cache.set_many({'a': 1, 'b': 2, 'c': 3})
cache.get_many(['a', 'b', 'c'])
cache.delete('a')
cache.delete_many(['a', 'b', 'c'])
cache.clear()
缓存视图函数:
template = loader.get_template('student_list.html')
result = template.render(data)
-----------------------------------------
cache.set('get_students', result, 60)
students_cache = cache.get('get_students')
4.使用redis实现缓存
①pip install django-redis
②用法和django内置的相同

猜你喜欢

转载自blog.csdn.net/dorisi_h_n_q/article/details/80551459
今日推荐