使用django-cms,如何允许用户指定背景图像 [英] Using django-cms, how can I allow the user to specify a background image

查看:80
本文介绍了使用django-cms,如何允许用户指定背景图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为客户创建django-cms网站。我想做这样的事情:

I am creating a django-cms site for a client. I would like to do something like:

<body style="background-image:url({% placeholder background-image %});">

所需的效果是在CMS用户可以选择一个背景图像的位置页。理想情况下,他们会选择使用 Filer 之类的现有图片。

The desired effect is to have a place where the user of the CMS can select a background image for a page. Ideally, they would choose an existing picture using something like Filer.

有没有办法做到这一点?

Is there a way to do this?

推荐答案

Paulo是正确的,第一步是配置一个占位符,使其最多只能使用一个图像插件。情况下, FileImagePlugin 。为此,可以修改 CMS_PLACEHOLDER_CONF

Paulo is right, the first step is to configure a placeholder so that it can only take at most one image plugin, in this case, FileImagePlugin. Do that by modifying CMS_PLACEHOLDER_CONF:

CMS_PLACEHOLDER_CONF = {
    'cover_image': {
        'plugins': ['FilerImagePlugin'],
        'name': _('Cover Image'),
        'limits': {'global': 1},
    },
}

确保在模板中,您正在某个位置显示此占位符:

Make sure in your template, you are showing this placeholder somewhere:

{% load cms_tags %}
{% placeholder "cover_image" %}

这将在< img> 标签中呈现图像。但是,如果您只想要图像的URL,该怎么办?这就是第二步。

This will render the image in an <img> tag. But what if you want just the URL of the image? That's what the second step is.

创建一个上下文处理器,它将直接为您提供图像。详细信息将根据您使用的图像插件进行修改,但这是我使用的插件。

Create a context processor that will give you the image directly. The details will modify depending on what image plugin you're using, but this is the one I used:

# in context_processors.py
from cms.models.pluginmodel import CMSPlugin

def page_extra(request):
    page = request.current_page
    if page:
        cover_image_plugin = CMSPlugin.objects.filter(
            placeholder__page=page,
            placeholder__slot='cover_image',
            plugin_type='FilerImagePlugin',
        ).first()
        if cover_image_plugin:
            return {'cover': cover_image_plugin.filerimage.image}
    return {}

请记住在您的 settings.py 文件中安装上下文处理器:

Remember to install the context processor in your settings.py file:

TEMPLATES[0]['OPTIONS']['context_processors'].append('example.context_processors.page_extra')

现在在模板中,您可以使用 cover.url 访问URL,如下所示:

Now in your template, you can access the URL using cover.url, like this:

<body
  {% if cover %}
    style="background-image: url('{{ cover.url|urlencode }}')"
  {% endif %}
>

这篇关于使用django-cms,如何允许用户指定背景图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆