Django static content through nfs on an intranet - django

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.

Related

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

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.

Push media files to a seperate nginx VM with Django in production

I don't know if I missed information regarding this. But I want to know how to store static files to a different VM. I've read that some recommend doing that to larger sites to seperate the load.
My current setup is that I use one computer engine with nginx, virtualenv, gunicorn etc. I use nginx to display the static files (including the media files) on the same server.
How can I push media files to a seperate nginx server when a user uploads an image? How can I obtain the same url as well?
Let's first explore the options of static resources(Javascript, CSS, Images, Fonts etc...)
You have complete control of where this static content should go during the deployment. Typically these resources will go for compression during the build process to optimize the content size to reduce the bandwidth.
Deploy the static resource on the different server(VM with ngnix) and configure that URL in the Django settings.py with STATIC_URL. If you use the cookieless domain, it will save little more bandwidth. Refer the section Use Cookie-free Domains for Components in this URL: https://developer.yahoo.com/performance/rules.html
You can use the content expiry settings for these resources to cache on the client for the specified duration.
Some best practices are mentioned in the official documentation: https://docs.djangoproject.com/en/1.10/howto/static-files/
For the media resources, you can save the files on the shared volume and run it on the different server same as static resources. You can configure that server URL in the Django settings.py with MEDIA_URL.

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 SSL cert and static media

I'm building a Django app that will need SSL on all user-facing pages. On other projects where SSL was required I've run into complications when serving media files from a different virtual host on the same server. For instance the page is: https://www.mysite.com but it's referencing http://media.mysite.com/css/screen.css, and the browser subsequently displays security warnings to the user.
My understanding is that it's Django best practice to keep static files on at least their own virtual host, which -- as far as I know -- requires a subdomain like media.blahblah.com.
Obviously there's plenty of Django apps on SSL, so I must be missing something. Any advice on how this is managed?
The general answer is that you'll need to change the URL you're using to reference your static files to one that uses HTTPS. Using a relative path (/static/css/screen.css) instead of an absolute URL (http://...) makes your media automatically switch from HTTP to HTTPS depending on the referring page, but does force your hand when trying to serve according to best practices described below.
If you're using Django 1.3 with contrib.staticfiles, it would appear that you would just need to change the STATIC_URL setting. If not, you'll have to update the paths manually (or however you are specifying your static assets).
Best practices for static media like CSS and JavaScript do dictate that you should serve them from a webserver (not just virtualhost) different from the one serving up your Django pages. The thought there is that you can use a low-footprint webserver to serve those simple files very quickly. If you serve them from the same webserver that is running your Django site, then most likely a number of extra modules are being loaded but not used for the requests where you're just serving a static file.
Since you need to serve secured static files, you have a couple of options:
You'll either need to get a separate (or wildcard) SSL certificate for your static files webserver.
Con: extra cost for the certificate
Con: you'll have to specify a different domain (instead of the relative paths described in the first paragraph) to serve your static files from.
Setup SSL on a reverse proxy that handles all of the requests for your site. You're still serving your static files and Django pages from separate webservers, but the proxy knows which one to connect to based on the URL or path (ex: proxy "/static" from the static webserver, all else from the Django webserver).
Pro: Does allow you to use relative paths to your media.
Con: Extra systems configuration.

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