jinja2 设置背景图像 [英] jinja2 set background image

查看:24
本文介绍了jinja2 设置背景图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 jinja2 设置背景图像.我已经设法从我的开发服务器提供我的文件,并且可以在文档中发布图像,但我希望它与顶部的其他所有内容形成一个漂亮的背景.

I am trying to set the background image using jinja2. I have managed to serve my file from my dev server and can post the image in the document, but I want it to form a nice background with everything else on top.

举个例子,我试过这个:

As an example, I tried this :

{% extends "layout.html" %}

{% block body %}
  {% if current_user.is_authenticated %}
    {% if commPageData.background_image %}    
        <body background="{{'/images/'+commPageData.background_image}}">
    {% else %}
        <body>
    {% endif %}
            A thing
         </body>

我有一个 css 文件和一个布局文件,它们提供了模板的默认行为.我怎样才能有一个漂亮的背景图片供我使用?

I have a css file and a layout file that gives the default behaviour for a template. How can I have a nice background image which I am serving?

推荐答案

您似乎试图告诉前端使用应用程序路由,而无需向前端传递正确的命令.我假设您正在使用 Flask.真的,有两种方法(我能想到)来切片这个面包,这取决于你.第一个只是使用标准的 html 而不是嵌入 python 来路由应用程序来查找文件,看起来像:

You seem to be trying to tell the front end to use application routing without passing it the right commands. I'm assuming you're using Flask. Really, there are two ways (that I can think of) to slice this bread, and it just depends on you. The first is just using standard html and not embedding python to route the app to look for the file, and looks like:

<body background="/path/to/image.jpg">

但是,如果您想利用框架和模板,那么您应该使用内置方法 url_for

But, if you want to take advantage of the framework and the template, then you should use the built in method url_for

看起来像这样:

<body background="{{ url_for('static', filename=commPageData.background_image) }}">

'static' 是应用程序中该图像所在的目录.

'static' being the directory in the application where this image lives.

现在,我不确定 commPageData.background_image 在您的应用中的来源.我的代码片段假定它被用作一个值,它会在传递回逻辑时识别它,如果这是有道理的,并且它需要是您告诉 url_for 方法查找它的位置,即使是动态生成的.如果您确实有想要渲染为背景的特定图像,则需要适当地指定它:

Now, I'm not sure where commPageData.background_image is coming from in your app. My code snip assumes it is being served as a value it will recognize if passed back to the logic, if that makes sense, and it needs to be where you tell the url_for method looks for it, even when dynamically generated. If you actually have a specific image you want to render as the background, it needs to be specified appropriately:

<body background="{{ url_for('static', filename='image.jpg') }}">

当然,在 HTML5 中已被弃用,可能无法在许多浏览器中正确呈现.最好使用 CSS 代替

Of course, has been deprecated in HTML5 and may not render appropriately in many browser. It is much preferred to use CSS instead with

<body style="background:url({{'images/'+commPageData.background_image}});"> 

或者直接放在你的 CSS 文件中

or put it directly in your CSS file

这篇关于jinja2 设置背景图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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