Python Django: Django won't render images from a folder outside static - python

Been working on a Django project for a personal portfolio. Inside the project I have different apps: a blog app and a projects app. When I try to render a images it wont display unless a use a specific path from the templates folder to the static folder. For example, based on my project directory, if I use "../static/img/example.jpg" it renders the image with out problem, but as soon as I use another path like "../../media/img/example.jpg" the image wont render and a blank space will appear. I would like to know is this a normal behavior with Django and if it is, then what is the best practice to display images, because nothing comes to mind right now.enter image description here

Django delivers files from static folder or the path we specified in STATIC_FILE_DIRS in settings. Media folder is intended to store the User uploaded files, not the static files. Again we have to set those in settings. This is the Django default behaviour.
You can use any cloud storage like AWS S3 to store files.

Related

Why practically the same path written in different forms doesn't works correct?

I am working in one django project.
I am trying to show image that is saved in Django models attribute (ImageFielf), and it's path is:
../projectname/appname/static/img/imgname.png
Considering that,I write in HTML code that:
class="result-item-preview fadeIn animated " style="background-image:url('../../projectname/appname/static/img/1202296.jpg'), url('../static/img/default-movie.png');
But this doesen't works.
It only works if it is written in usually form:
class="result-item-preview fadeIn animated " style="background-image:url('../static/img/1202296.jpg'), url('../static/img/default-movie.png');
How to solve this problem ?
In the Django docs they advise to use static template tag,
https://docs.djangoproject.com/en/3.1/howto/static-files/
style="background-image:url({% static 'img/1202296.jpg' %}), url({% static 'img/default-movie.png' %});"
Now about the question itself: why the first method is not working? This is because the server that serves your web app has no clue about the directory structure in your Django project, hence it doesn't know where to find '../../projectname/appname/static/img/1202296.jpg'.
The second method is working because you defined the location of static files in the Django project settings, so the web server knows how to find static files and how to server them.

Flask login_required to access sub-directory within static folder

app.view_functions['static'] = login_required(app.send_static_file) is working if I want to prevent access to the static folder without being logged in.
However this prevents the style.css being accessed. I want to prevent people accessing a subfolder of my static files Static > Images. I have tried to do this as follows:
app.view_functions['static/Images'] = login_required(app.send_static_file)
This isn't working, i.e. I can still access static/Images/... without being logged in.
If protecting a subdirectory isn't possible, can I change the location of my style.css to be outside of my static folder?
I am trying to protect sensitive Images being available to anyone with the path to the image. I can prevent send_from_directory being used without log in, but I also do not want someone with the www.___./path_to_image to be able to access it.
Many thanks
I think the way to go is to add the files to a folder different from /static (which is supposed to be public), as explained in this answer:
Restrict static file access to logged in users

Django - Is it possible to show image on page without make it static?

I'm building a web page to modify images(cropping, scaling, etc..) based on the meta data which is the output of my python scripts. In each meta data specify the index of the particular image in the field (ex: meta data: [idx: 01, ..,..] then image01 will be operated.) However in my case, there are tons of image source(dataset_01, dataset_02, ...) and in each image dataset it contains 10000 images. Now my solution is to put the image dataset into the static folder and command
python manage.py collectstatic
However it took tons of time as django has to collect all the 10000 image source everytime I add a new image dataset. I'm just wondering is there any possible that I don't have to collect those images as static file and just directly open the image files in the folder?
This way can be solution:
in settings.py:
STATIC_ROOT = os.path.join(BASE_DIR, 'static_root')
then locate your static files on 'static_root' folder.
then if django is running with DEBUG=False, it will serve static files on that folder with STATIC_URL.
note: if you want to serve static file with django runserver, you have to add --insecure, so like this: runserver --insecure

Display a set of Images from a folder on Web2py

I am a web2py newbie and I have the following doubt.
I have a web2py application which take some inputs from the user and then generate some images. These images are then stored inside a folder under the private folder.
I want to display these images on the web2py application.
How do I do that? Also, I don't want to upload the images as a database but simply read them from the folder and display it directly in the application. I got a reference from here:
http://www.widecodes.com/0xmqqVkXkP/web2py-downloading-files-displaying-images.html
But, couldn't quite understand it.
Any help?
Thanks in advance!
Assuming you know the file name and path, you can return any file to the browser via a controller action that calls response.stream. In a controller (e.g., default.py):
import os
def serve_file():
filename = request.args(0)
path = os.path.join(request.folder, 'private', 'file_subfolder', filename)
return response.stream(path)
In a view, you would then include an image as follows:
<img src="{{=URL(default, serve_file, args=filename)}}" />
If the images are intended to be public, a better option is to store them in the /static folder -- any files in /static can simply be served directly via their URL:
<img src="{{=URL('static', 'image_files', args=filename)}}" />
This method does not require a special controller action to call response.stream.

Django Template Inheritance -- Missing Images?

I have got the following file heirarchy:
project
other stuff
templates
images
images for site
app1
templates for app1
registration
login template
base.html (base for entire site)
style.css (for base.html)
In the login template, I am extending 'base.html.' 'base.html' uses 'style.css' along with all of the images in the 'templates/images' directory. For some reason, none of the CSS styles or images will show up in the login template, even though I'm extending it.
Does this missing image issue have something to do with screwed up "media" settings somewhere? I never understood those, but this is a major roadblock in my proof-of-concept, so any help is appreciated.
Thanks!
I recommend not putting the styles and images there. For development, your MEDIA_ROOT may point to an arbitrary local directory on your machine containing the files, it need not even be below your django project's root folder (see http://docs.djangoproject.com/en/dev/ref/settings/#media-root).
For production, you'll have to choose a different approach to serving static content anyway (http://docs.djangoproject.com/en/dev/howto/static-files/#the-big-fat-disclaimer).

Categories

Resources