Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set background image on Flask Templates?

My background-image works only for this template that has @app.route('/').

 <header class="intro-header" style="background-image: url('static/img/home.jpg')">

This works perfectly fine when:

@app.route('/')
def home():
    return render_template('post.html')

Everything works. I get this:

127.0.0.1 - - [19/Sep/2016 21:07:11] "GET /static/img/home.jpg HTTP/1.1" 304 

But when I use same template with:

@app.route('/post/')
def post():
     return render_template('post.html')

I get this:

127.0.0.1 - - [19/Sep/2016 21:15:23] "GET /post/static/img/home.jpg HTTP/1.1" 404 -                                                                          

And background-image is blank.

like image 622
Yakuzhy Avatar asked Sep 19 '16 18:09

Yakuzhy


People also ask

How do you get a background image on a Flask?

Originally Answered: How do I set background in CSS via flask? Try setting it something like this: <body style="background:url({{'images/'+commPageData. background_image}});"> or just put it in the stylesheet directly.

How do I display an image in HTML Flask?

To display image on a HTML page with Python Flask, we can pass the image path to the template from the view. Then we call render_template with the template file name, and the user_image argument set to the image path. to interpolate the user_image in the template.

How do I use render_template in flask?

from flask import render_template Using the render template within a URL endpoint: render_template ('< template file name>', < variable_1> ='< value of the variable 1 >', <variable 2>=< value of the variable 2 >) Here everything within the ”< >” needs to be replaced with the variable that goes as an input to the HTML template file.

How do I create a template in flask?

Flask looks for templates in the templates directory, which is called templates, so the name is important. Make sure you’re inside the flask_app directory and run the following command to create the templates directory: Next, open a file called index.html inside the templates directory for editing.

How the error message is displayed in flask?

How the error message is displayed will be handled by the presentation logic. In Flask, you can use the Jinja templating language to render HTML templates. A template is a file that can contain both fixed and dynamic content.

How to create a flask server in Python?

The very first step is to install Flask in the Python environment we created. We can achieve this by pip install command i.e. pip install flask. Once the required modules are installed, we would need to create a base Flask application, which will be sufficient to run the Flask server.


1 Answers

This is a simple problem can solved by Flask documentation

Anyway, you should use something like this in your template:

background-image: url({{ url_for('static', filename='img/home.jpg') }})

but if you don't want to use Flask methods use :

url('/static/img/home.jpg')

or use another web server instead of flask default web server for your files like Apache and access via http://yoursite/static/img/home.jpg

like image 105
Hamed Mahmoudkhani Avatar answered Sep 26 '22 00:09

Hamed Mahmoudkhani