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.
Related
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.
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
I have a django app (my_app) that based on the user query:
creates a file from the db
runs a program on the file from step-1 and gets an output file
generates a json file from the step-2 output file
renders a D3 visualization from a django template using the data from the json file from step-3
I need the program to run on the server side and the json file to be generated server-side as well.
Because the json files are query-specific, I thought it's not a good idea to keep these files in the /static/ folder and thought of keeping the files (even if temporarily) in e.g. /myapp/output_files/ folder.
The problem is that there is no url pattern corresponding to /myapp/output_files/my_file.json and I get a "Page not found (404)" error if I try to open the generated file and it obviously doesn't load in the javascript code in the template.
Is there a better way to design the system?
If the design is ok, how can I access a json file in the app's folder from the django template? Do I need something in the urls.py?
P.S. Everything works fine if I change the json location to /static/ or its subfolder.
Just add the location to your STATICFILES_DIRS setting as shown here
However, you probably need to build a view function that can somehow return the json based on some parameter in the url. Static files are meant to stay static...
I'm pretty new to Python\Django.
I'm trying to make a local Netflix-like Library for my Movie\TV shows collection.
The general idea is that the user chooses a media folder, the server side runs on the files in the folder adds them to the database and then the user can search for the items and play them back in the GUI.
The first snag I ran into is getting the folder path from the user without actually uploading any files. After doing some searching online I found this :
<input type="file" id="file_input" webkitdirectory="" directory="">
This HTML code allows the user to choose a folder and iterates through all the files inside, however, I don't know how can I pass this information to views.py so that I could run logic on the input.
Does anyone know how this could be accomplished ?
For security reasons, browsers do not allow to get folder path (Stack Overflow). Since you do not want the user to upload files, the possible solution would be to explicitly mention the folder path in <input type="text">. The simplest solution would be python –m SimpleHTTPServer (source, docs). You may be also interested in this Django app.
I am able to access the static file in question via direct url (localhost:8000/static/maps/foo.txt), so I guess I have it all working well. But I can't do the following: I want to open that text file in views.py. It's because I'm working on a simple web browser adventure game and I wanted to store maps in static/maps and load those maps using f=open('/static/maps/' + mapname + '.txt', 'r'). I get the IOError: no such file or directory. I really don't understand it, because there is such directory when I search for it in address.
Can it be done somehow?
You need to use the place they are stored on disk, which is probably in settings.STATIC_ROOT or settings.STATICFILES_DIRS, not the place they are being served by the web app.
Note however that if you are modifying these files programmatically, they aren't (by definition) static files. You'd be better off using the MEDIA_ROOT location. Also note that Django has helpers to do this sort of thing - see the documentation on Managing files.