Flask-Images is not working - python

I installed Flask-Images to dynamically resize images according to the doco here: https://mikeboers.github.io/Flask-Images/
My code is like this:
Jinja
<img src="{{ resized_img_src(filename, width=200) }}"/>
And in views.py
#app.route("/uploads/<filename>", methods=["GET"])
def uploaded_file(filename):
return send_from_directory(upload_path(), filename)
The images do not change size even though I can see that the modified url is being returned on dev tools:
<img src="/uploads/69?w=200&s=4tDuOKq1G1qURVty_6pYAxpG4jk">
I know something similar was asked here a while ago Flask-Images does not work
but the only answer there wasn't helpful.
I've been at this for 2 days now. If anyone has experience incorporating Flask-Images into their project, I'd appreciate any suggestions. Would people recommend using Flask-Resize instead? https://github.com/jmagnusson/Flask-Resize.

The Issue
The problem with your current setup is that Flask-Images can't find the image file 69.
If you run your Flask application in debug mode, you would see the following 404 occur when you entered the URL with the image to be resized in it.
127.0.0.1 - - [04/Jul/2015 17:09:50] "GET /imgsizer/uploads/beastie.png?w=200&s=3mNDiQA6kRKps70a7xBi0BywF2w HTTP/1.1" 404 -
Solution
For flask-images to be able to load the resource, we need to put it somewhere accessible, that would be the 'static' directory by default in Flask
The solution as stated above would be to either move the images into the static directory, you can also override the directory path in the app definition, or in a setting
app = Flask(static_folder='some_other_path_with_images')
app.config['STATIC_FOLDER'] = 'or_this_other_directory'
After fixing this, you can also check the app debug console and see the requests are going through just fine
127.0.0.1 - - [04/Jul/2015 17:18:47] "GET /imgsizer/beastie.png?v=VZfrJw&w=100&s=2_gsJZCVk-gvUm4VD-OD6ZuXjbY HTTP/1.1" 200 -
Source Code
Feel free to take a look at the model i set up to check this out, the solution for this problem can be found here on github.

Related

Flask can't find local static files even though they exist and the specified path is correct

I use render_template in conjunction with Flask to render an html file, index.html, which in turn references a CSS stylesheet, "templatemo-style.css". Within that css file, references are made to other local files like images and so on.
The folder structure looks like this:
/PARENT FOLDER
/templates
/index.html
/static
/assets
/images
/various files
Within the "assets" folder, there is sub folder called "css" which houses the css file.
The css file makes a call to the following url to grab the website's banner image:
background-image: url(../assets/images/main-banner.jpg);
The problem is, when I RUN the flask application, I get the following 404 error in my terminal:
127.0.0.1 - - [02/Aug/2021 13:13:51] "GET /static/images/main-banner.jpg HTTP/1.1" 404 -
For some reason, the application isn't calling the path that I specified in the code. I've searched all over the code in various different files for the place where this mysterious call is made and I can't find it anywhere. Even if I comment out my "background-image:" line above, I still get the same error. I should note that for the front-end i'm using a css/bootstrap template that I found online and then modified to my liking. I've searched the js code as well and that call isn't made there either.
I'm completely stumped and have no idea where this mystery call is coming from. Anybody have any possible ideas? Thanks
EDIT: figured it out. Its because my browser was caching my css file and therefore not running updated code.
Maybe this would work
background-image: src="{{ url_for('static', filename='filename') }}"

Django does not find static on all pages except the main

The project is hosted (support say to solve the problem yourself and therefore my friends hope for your help) and you can find it at: https://caparolcenterspb.ru
All styles, pictures and js appear on the main page, but not on other pages. You can see the errors directly in the browser, but just in case I give a screen:
It seems you are adding the path to the static file not from the STATIC_ROOT, but from the current URL. I'll explain with the example.
On the main page, you got the link:
https://caparolcenterspb.ru/
And looking for the static logo at:
https://caparolcenterspb.ru/static/img/Logo.svg
And all works fine. But if you will go to
https://caparolcenterspb.ru/services/
The URL for the logo request will change to:
https://caparolcenterspb.ru/services/static/img/Logo.svg
Which is wrong and there are obviously no files on this link.
So, the solution to your problem will be fixing STATIC_URL in Django settings. You need to use the same fixed STATIC_ROOT for all static requests and everything will be fine.
It is pretty simple, just check everything is done as it described here:
https://docs.djangoproject.com/en/3.0/howto/static-files/
The shown error explains your MIME is text/html not text/css. It's linked to your browser or OS.
Try to add this in your settings.py:
import mimetypes
mimetypes.add_type("text/css", ".css", True)

Jinja2 not work on flask/glitch.me deployment

I have a deployment of a tiny Flask app on Glitch. It seems to be working... sort of. However, the Jinja2 template engine is clearly not working, since all Jinja2 tags are being read into the browser as text. The application, which I have up on my GitHub page, works fine on the localhost. Can I fix this somehow?
Seems like this was not a problem with earlier versions of Flask and Python.
Firstly, adding the following debug option in the server.py file.
app.debug = True
Secondly, add some code as follows:
#app.after_request
def apply_kr_hello(response):
"""Adds some headers to all responses."""
...
response.headers['Cache-Control'] = 'no-cache'
return response
Thirdly, do a Ctrl-Shift-R on the specific browser tab where your application is being rendered.
That is it! Now, when you make changes to static file, they should be reflected in your app.
I presume one might also benefit from reading https://stackabuse.com/serving-static-files-with-flask as well.
I'm not a python expert but while your glitch project has the same code as localhost, your local machine is executing the code through a different path. In this case because there are no instructions for Glitch to start your project from mesostic.py it's just treating your project as a static website. (hence why the template tags are just being rendered out as text).
It might help to look at other python projects on glitch to get a sense of how to execute your .py file. e.g. checkout start.sh on https://glitch.com/edit/#!/python3-morepath. I'm not sure but you may also need a requirements.txt to tell Glitch that this is a python project.
Hope this gives you a place to start debugging from

CSS not being applied in Flask app

I am using flask and Jinja templates. None of my CSS is being applied, except for bootstrap which is being downloaded from an external host.
Here is the line in my base template for my own stylesheet:
<link rel=”stylesheet” type=”text/css” href="{{ url_for('static', filename='style/stylesheet.css') }}">
And then when I open the page in chrome I can follow the link, and it opens up the file successfully. However when I look in the frames I cannot see my stylesheet under the stylesheets section:
Here is the request from the server: GET /static/style/stylesheet.css HTTP/1.1" 200 -
It looks likes it's not being recognized as a css file ? I'm not great with web stuff so hopefully this is something simple.
I have no idea what was happening here. This issue has been ongoing for days.
To fix it I simply copied the line that loads the CSS, saved my project, pasted it back in, and ran the server. Mass confusion.
For anyone else still having issues with this I found this that suggests the CSS is not being "hard refreshed".
I fixed this issue on my Mac in Chrome by holding down both ⌘ Cmd+⇧ Shift and pressing R.

Django: Page doesn't load images

I am working on a Django project for a company. This project worked very well before today.
Today I found a page can not show images (and their corrsponding links). I checked source code of THAT PAGE, I found there are images and links, I just can not find them on the page.
I checked the auth of the server and I am sure I can write things to the database. In fact, I think it is not database mistake because I can find what I want in the page source code, I just can not find them on the page.
Oh my Gosh, I am going to be crazy...
Has anyone suffered similar problem? What kind of problem could it be?
Please help me! Thank you very much!
PS: I can not provide any source code of the project because some business limit...I am really sorry...
Try exploring the site_media directory. If you're images are being served up as static content it could be related to the permissions of that folder on local disk or based on the settings.
Within your urls you may have something similar to:
(r'^site_media/(?P<path>.*)$', 'django.views.static.serve',
{'document_root': '/path/to/media'}),
OR it may reference the path in the settings file something similar to:
STATIC_DOC_ROOT = '/path/to/media'
If the link looks correct for the image, but the side is not rendering the image even directly your issue will be somewhere in these areas.
For more information check the django docs:
http://docs.djangoproject.com/en/dev/howto/static-files/

Categories