How to display arbitrary images of a server in Django 2.1? - django

Have a web interface.
I can select any path of the server on the web interface.
Display all the images in this path to the web interface.
try:
I can't use these images as static resources.
If I take the absolute path of the image as the 'src' of the
tag, it will cause the image 404 error.
What should I do if I want to make this image's url correct?

You can not, by design. Django uses Storage engines to handle files it has access to (even files using remote services).
To reference static files, using the {% static 'relative/to/static.jpg' %} template tag, you need to use the included staticfiles module.
To reference uploaded files, you can use {{MEDIA_URL}} as illustrated the documentation.
The reason for this approach in Django is that it protects against attempts to reference files outside the base location of the Storage engine and throw a SuspiciousOperation exception.
Naturally, you can write your own Storage engine and reference files in any way you want.
And one could use any of these packages to manage them.

Related

Sharing files from google cloud storage to GAE

I have one Django application running GAE.The application uses content folder which contains images and html snippets.The content folder was uploaded in google cloud storage.I would like to render a image in static file using img tag.For using img tag I want to know the url of that image.I have seen that when we set the permission to share publicly it will give us a url.But I don't want to share that files publicly.If I share an another application can use my files.I don't want that.I there any way to do that with out log in a user
Sharing it publicly is the best way to go.
You could also base64 encode the image data when you render out the template, which means the url of the image will not be shown to the public on your page. Then you can obfuscate the image names in the GCS. This way it's still public but hard to reach.

Django serve files outside the web root

I currently have Django set up to upload files to:
/path/to/project/uploads
This works great. This folder is in the root folder of the project so the files cannot be served directly from a web URL, which is what I want, the files are "CVs" uploaded by users.
I've had a look at a third-party django app called filetransfers which would do the job, but I'm wondering if there is a way with Django core to serve files from outside the media folder.
Any help would be great.
Andy
Depending on what web server you are using I would recommend using X-sendfile if you use Apache or X-accel-redirect if you use Nginx. But remember you will need to change setting in your web server. But this is far more efficient way of serving files than using Django to do it.
If what you want is to keep control on how your files are served / who can see them etc, then the simplest solution is to write a custom view serving theses files. You just have to provide the file's content as the response body and set the appropriate response headers (file type, content length etc). Reading the FineManual(tm) part about the Response object should be a good starting point.
Resolved using FileWrapper().
Thanks anyway.

serving static files on a separate server

I'm creating a website with django. There isn't much static content ( maybe 20 images, and 5-10 css/javascript docs).
I read up on Managing Static files in django. Do I need to deploy my static content on a separate server, or will it work fine since I have very little static content? currently, I'm accessing all my css files and images with the actual path name instead of using "{{STATIC_URL}}".
First you need to use {% static %} to access static files. Please see Django's official docs on this.
Answering your question: you do not need to keep your static files on a separate server but it is highly recommended. The main reason is performance. They will be served directly from HTTP server avoiding additional load on application server. Also, they will be cached by server/client.
You can find a lot of article on this topic. Also check official docs: deploying static files.
You will at least need an HTTP server running on whatever you're running your django project from, and it's highly recommended that you use a separate server for your static files apart from your app logic.
Secondly, it's very bad practice not to use {{ STATIC_URL }} or a similar item. Absolute paths are evil. If the project changes machines, or if it needs multiple versions, etc. These paths could very well change.

Django static content through nfs on an intranet

I'm deploying a Django application that is based on a whole lot of static content. All of the computers using the application are on an intranet, with the static content available through nfs.
Can django be configured to let users get the static data through their nfs mounts, rather than forcing it all through a web server?
If I've got this in a template:
<img src="/path/to/img.png"/>
The browser is requesting that image from
http://localhost:8000/path/to/img.png
What I'm trying to do is get the client to treat that as a local path, rather than asking a server for it.
To reference a file in your file system (NFS mounted or otherwise), the URI to use is file:///path/to/file. However, that will not work in your case -- <a href="file:///path"> will only work from a static HTML file loaded from the local filesystem.
For security reasons, a web page is not allowed to access the local filesystem. See Why can't I do <img src="C:/localfile.jpg">?
If you want to speed up access to static files, one option would be to set up a separate light-weight webserver that's dedicated to service your static files (perhaps nginx? or lighttpd? or mongoose?) and use the URL of that server as your MEDIA_URL.
Well, you should try it out. If django is treating the static content path as just another constant, then your network share path should work just well. But, if it isn't, then you can try defining your own constant like: static_on_network_share or something and use that constant while loading content in templates/html.

Django where to put static files

I am using Django to create a small web app, however I do not know where i must put my HTML and JS files. I don't want to use the templateing system because I have no need to pass the values from Django directly to the HTML template, Instead the HTML will be static and I will fetch all the data necessary and send the data to be input back into the database using AJAX with Jquery.
My Question is where must I put my HTMl and JS files so they are accessible from the web browser and will be in the same directory so that I can send my ajax requests to something like
http://localhost:2000/webapp/RPC/updateitem/ (more stuff here)
and where the HTML files are
http://localhost:2000/webapp/index.html
Thanks,
RayQuang
You let your main webserver (the one you're running django on) deal with the static files. In most cases this means that you simly server the files through apache (or lighttpd or cherrypy or whatever). Django is only ment for the rendering of dynamic things and thus should not be used for serving static files.
If you're running from a development server (which I can't recommend), this tutorial will help you through setting it up: Serving static files