Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/277.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何在django cms中添加背景图像?_Python_Django_Django Cms - Fatal编程技术网

Python 如何在django cms中添加背景图像?

Python 如何在django cms中添加背景图像?,python,django,django-cms,Python,Django,Django Cms,我正试图在使用Django CMS构建的网站上设置标题的可编辑背景图像 尝试了来自的说明,但在生成的HTML代码中仍然得到了“background image:url(“”)” 下面是我为添加背景图像而添加的代码: 项目根目录中的context_processors.py: settings.py结束: base.html: 。。。 {%占位符“封面图片”%} ... 有人能帮我修复它并使背景图像正常工作吗?在您的项目中,添加到您的模型中。py: from cms.models import

我正试图在使用Django CMS构建的网站上设置标题的可编辑背景图像

尝试了来自的说明,但在生成的HTML代码中仍然得到了“background image:url(“”)”

下面是我为添加背景图像而添加的代码:

  • 项目根目录中的context_processors.py:
  • settings.py结束:
  • base.html:
  • 。。。
    {%占位符“封面图片”%}
    ...
    

    有人能帮我修复它并使背景图像正常工作吗?

    在您的项目中,添加到您的
    模型中。py

    from cms.models import CMSPlugin
    from filer.fields.image import FilerImageField
    
    class MyCoverModel(CMSPlugin):
        cover = FilerImageField(
            null=True,
            blank=True,
            related_name='header_logo',
        )
    
    cms\u plugins.py中添加:

    from cms.plugin_base.CMSPluginBase
    from cms.plugin_pool import plugin_pool
    from .models import MyCoverModel
    
    class CoverPlugin(CMSPluginBase):
        name = "Cover"
        model = MyCoverModel
        render_template = 'my_cover.html'
    
    plugin_pool.register_plugin(CoverPlugin)
    
    {% load thumbnail %}
    
    {% thumbnail instance.cover 1000x500 crop upscale as thumb %}
    <header class="masthead" style="background-image: url('{{ thumb.url }}'); width={{ thumb.width }}; height={{ thumb.height }};"></header>
    
    模板/my_cover.html
    中添加:

    from cms.plugin_base.CMSPluginBase
    from cms.plugin_pool import plugin_pool
    from .models import MyCoverModel
    
    class CoverPlugin(CMSPluginBase):
        name = "Cover"
        model = MyCoverModel
        render_template = 'my_cover.html'
    
    plugin_pool.register_plugin(CoverPlugin)
    
    {% load thumbnail %}
    
    {% thumbnail instance.cover 1000x500 crop upscale as thumb %}
    <header class="masthead" style="background-image: url('{{ thumb.url }}'); width={{ thumb.width }}; height={{ thumb.height }};"></header>
    
    {%load缩略图%}
    {%thumbnail instance.cover 1000x500以thumb%的形式进行高比例裁剪}
    
    注意应用程序中templatetag缩略图的用法。在这里,它将图像大小限制为1000×500像素


    这将为您的CMS安装添加一个简单的插件。通过编辑占位符字段,它提供了一个名为
    Cover
    的插件。从那里选择一个图像,它将被呈现为标题的背景。

    如果你在模板中使用它,是否打印任何内容?@AmanGarg,否,否当问题出现在你的
    上下文\u处理器.py
    中时。你应该调试每一行并检查你的
    cover\u image()
    是否返回了一些东西。这不是插件的实际使用方式。如果你有一个插件,你可以像以前一样将
    占位符
    添加到模板中,但是插件自己的模板&
    render
    会将
    cover
    传递到上下文中并包含标记。根据您所包含的内容,您可能应该将其作为页面扩展来执行。您可以通过扩展页面模型为每个页面提供一个图像@马克,谢谢你的帮助。尝试使用您引用的手册。实际上一切都好。我已经创建了一个带有页面扩展的应用程序,唯一不好的是路径有问题。在模板中,我使用“{%static request.current_page.coverimageextension.image.url%}”,并在生成的HTML中得到“/static/media/cover_images/contributive-mountain-names-featimg_mod.jpg”,但图像位于另一个路径-“/data/media/cover_images/contributive-mountain-names-featimg_mod.jpg”。在models.py中,我使用“image=models.ImageField(上传到class='cover_images')”。你能帮我修路吗?
    {% load thumbnail %}
    
    {% thumbnail instance.cover 1000x500 crop upscale as thumb %}
    <header class="masthead" style="background-image: url('{{ thumb.url }}'); width={{ thumb.width }}; height={{ thumb.height }};"></header>