python - 从代码自定义 CSS (Django)

标签 python css django

我正在 django 上编写应用程序,并希望实现更改 CSS 设置的功能。例如,在我的设置模块上,我想更改字体的颜色或字体的大小,知道如何实现吗?

谢谢!

最佳答案

我正在使用自定义 View 返回动态 CSS 文件;它使用文件缓存来避免为每个请求重复渲染。

查看函数:

@cache_page(None)  # won't expire, ever
def dyn_css(request):
    (css_theme, created) = CSS_Theme.objects.get_or_create(pk=1)
    return render_to_response('dynamic.css', {'theme': css_theme},
                              content_type="text/css")

重置功能,需要时“手动”调用:

def reset_cache(request, view='dyn_css', args=None):
    from django.core.cache import cache
    from django.utils.cache import get_cache_key

    if args is None:
        path = reverse(view)
    else:
        path = reverse(view, args=args)

    request.path = path
    key = get_cache_key(request)
    if key in cache:
        cache.delete(key)

CSS 文件:

.row {
    background-color: {{ theme.background_color }} !important;
}

关于python - 从代码自定义 CSS (Django),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28030098/

相关文章:

python - 从 for 循环中获取平均值

javascript - 插入/删除新帖子时转换其他帖子

django - 根据请求类型更改 Django REST Framework ModelSerializer 中的字段?

python - 如何修复 numpy 浮点运算产生不精确结果的问题?

python - 我如何通过电子邮件将来自 Flask 的错误日志发送给自己?

css - 无法将 Angular Material md-sidenav 设置为 100% 高度

css - 删除文本中的蓝色突出显示

浏览器中的 AJAX 命令行界面

python - 尽管安装了 GEOS,但获取 "django.core.exceptions.ImproperlyConfigured: GEOS is required and has not been detected."

python - 导入错误: cannot import name 'ft2font' from partially initialized module 'matplotlib'