Beginner: Django dev server external static files - django

Disclaimer: I am a beginner to Django but have Drupal programming experience.
I have spent some time Googling but can't find the answer to this question: How can I make the Django dev server show images (thumbnails of TIF's in my case) that come from outside the STATIC_ROOT and MEDIA_ROOT of python manage.py runserver 80? For example:
# something.html -- a template file -- WORKS AS EXPECTED BUT SEE NEXT EXAMPLE
<img src="http://www.google.com/someDir/someFile.TIF" height="y" width="x">
# BETTER EXAMPLE -- hdrive COMES FROM USB INSERTED AT RUNTIME
<img src="/hdrive/someFile.TIF" height="y" width="x">
Thanks!

This doesn't make any sense.
Remember that <img src=... is a part of the rendered template that is sent to the client's browser. Why would we want the client to be able to fetch any file in any directory they wanted from the server? (i.e.download $$$)
STATIC_ROOT and MEDIA_ROOT are two designated folders on the server that hold any files we want to be accesible from the outside, i.e. the clients browser.
On the other hand, before or during rendering the template (i.e. before the HTML file is sent from the server to the client) we can open files from outside these two directories using simple python file reading techniques.
For example, we could open an image saved in a directory outside MEDIA_ROOT or STATIC_ROOT, do something to it and copy the result into the MEDIA_ROOT meaning that the client could then read that file at /media/copied_file.png
It seems you are getting confused between the various stages of the request cycle.

Related

Mapping two MEDIA_URLs to the same MEDIA_ROOT

I’m migrating a website from WordPress to Django/Wagtail. I have all the old WP content in my media directory & it’s all being served appropriately.
It would be convenient to map other URLs (specifically /wp-content/) to MEDIA_ROOT, for the sake of old media URLs that were hardcoded in the content.
So for example a migrated asset now available at //example.com/media/uploads/2017/12/IMG_2120.jpg can also be served from //example.com/wp-content/uploads/2017/12/IMG_2120.jpg
I’m sure there’s some obvious way to do this (in urls.py?) but totally drawing a blank.
I'm sure you already know that static/media files should be served using a frontend server (like Nginx), because it's been mentioned at so many places in the docs.
So, if Django doesn't serve the files, why does it need the MEDIA_ROOT and MEDIA_URL settings?
MEDIA_ROOT is the place where Django stores the images/files you upload.
MEDIA_URL is used by Django to generate file urls. For example, if MEDIA_URL = '/media/', then if you do {{ image.url }}, Django will generate a url like this - /media/image.jpg.
But Django doesn't serve the files. Your frontend server does. So, what you do is, you configure your frontend server like this:
if request path starts with /media/:
map it to <media directory>
Basically, you're telling your frontend server to serve content from the <media directory> for every request that starts with /media/. This way, a request starting with /media/ never actually reaches your Django app, because your server is taking care of them.
What I mean by all this is that you can configure your frontend server to map /wp-content/uploads/ to your <media directory> and it will serve the files.

Deploying a Django site with sensitive code on a host

I've been developing a site with sensitive (i.e. proprietary) code on my local machine, testing it using apache2, and I'm finally going to be getting it setup with a web host. I'm a bit wary because of the "Where should this code live?" note here in the Django Tutorial:
Where should this code live?
If your background is in plain old PHP (with no use of modern frameworks), you’re probably used to putting code under the Web server’s document root (in a place such as /var/www). With Django, you don’t do that. It’s not a good idea to put any of this Python code within your Web server’s document root, because it risks the possibility that people may be able to view your code over the Web. That’s not good for security.
Put your code in some directory outside of the document root, such as /home/mycode.
My host told me that I'll be given a /home/ directory, and that the site will live in /home/www. I'm trying to emulate this directory structure on my end before I send everything to him to make sure it goes as smoothly as possible. My question is, if I want all my code to live outside of the /www directory (per the Django tutorial recommendation above), what actually goes inside the /www directory?
My development directory structure is basically this:
project
db
app1
app2
mysite (contains settings.py, wsgi.py, etc.)
static
templates (contains my base.html, and custom templates for admin, etc.)
Where app1 and app2 are Django apps I've developed to plug into mysite. So what folders / files need to go in the home/www directory, and what can safely live in home/mycode?
Your static and media (user-uploaded) folders, robots.txt etc. should be in the www directory. Basically any file that is directly served by your webserver and not through Django. Other files should live outside of this directory.
Your webserver should point all requests that are not found in the www directory towards your wsgi application, which doesn't need the code to be accessible by an url.
The reason for this is that your webserver does not execute the code in a python file, in contrast to php files. If your code lived in your web root, people could read your settings files by just going to example.com/src/settings.py. Images, plain html/text files and javascript should be read, but any code that should be executed should live outside your web root. Django will execute the files and generate the response that a user should actually see.

Access Django App from subdomain redirect

I have a small django app running onn some rootserver. Its using the django integrated dev server. The url is known only to a few people so think thats ok for now. We can access the website by:
http://<ip>:<Port>/main
A colleague has set up a subdomain like shortcut.somedomain.com that points to the above url.
When trying to access the site via that subdomain the view and template are loaded, but it fails loading the static files. Is there any quick fix to make it work?
Can you show us the code that states which are the static files directories and an example where you use a static file in a template?
Is it possible you're loading the static dirs from a wrong path?
Or maybe you use refer to the files incorrectly in the template?
try doing:
my_path = os.path.realpath(os.path.join(os.path.dirname(__file__), '..'))
this will get you the project root.
after that(make modifications if your project structure is different):
STATIC_ROOT = os.path.join(my_path, 'static')
and this should work with your STATIC_URL definition

Heroku Django Static

Do static images expire after soon point and automatically get deleted?
My situation is that I retrieve images from my PostgreSQL database through a model's ImageField by setting its upload_to equal to static/images. Then, I access the images through mysitesurl.com/static/images/model.url This works perfectly initially. However, after several hours, what I notice is that the images are no longer accessible. When I try to access them through the same url, they no longer exist. I do not do any manual image deletions so the operations I perform should not interfere with this.
Is this something that Heroku does that I do not understand?
Also, one odd occurrence that I notice is that the image is still accessible through the url, but it doesn't actually get saved to static/images. At least, when I run heroku run ls static/images, it is not there. Is this the correct way to check a directory's contents in heroku?
EDIT: My configs
STATIC_ROOT = ''
STATIC_URL = '/static/'
STATICFILES_DIRS = (
'static',
'static/uploaded_stuff/',
)
You can run heroku run bash --app YOUR_APP from your repository directory. This creates a shell to your Heroku instance allowing you to navigate around your files.
You can check on your static images that way.
I would suggest using S3 to serve your static images though. You can view this tutorial on how to integrate it:
http://blog.doismellburning.co.uk/2012/07/14/using-amazon-s3-to-host-your-django-static-files/
After further details, you are trying to allow user uploads and Heroku's file system is read-only. You will need to use S3 or a similar hosting solution for static/uploaded files.
https://devcenter.heroku.com/articles/read-only-filesystem

Serving static media in django application

I notice that when I reference my java scripts and static image files from my templates, they show up in development, but not from the production server. From development, I access them as such:
<img src="/my_proj/media/css/images/collapsed.png" />
but from production, I have to remove the project directory:
<img src="/media/css/images/collapsed.png" />
I'm assuming I'm doing something wrong with regard to serving static media.
I'm caught between a number of seemingly different options for serving static media in Django. On one hand, it's been recommended that I use django-staticfiles to serve media. On the other I see reference to STATIC_ROOT and STATIC_URL in the documentation (with caveats about use in production).
I have small .png files of "plus" and "minus" symbols for use in some of my jQuery scripts. In addition, the scripts themselves need to be referenced.
1) Am I correctly categorizing scripts and site images as static media?
2) What is the best method to access this media (from production)?
You shouldn't keep the URLs hardcoded that way.
If you're running thedev version of Django, you should check this doc.
If you're not, for simplicity, you can use {{ MEDIA_URL }} and configure that variable in your settings for dev and for production.
<img src="{{ MEDIA_URL }}/media/css..." />
Keep in mind that you'll have to use RequestContext, see more here on docs here and here.
Also, you should server all your static files directly trough a proper webserver. You can configure apache or nginx to do that.
Sigh. There is only one way to serve static media in production, and that is to get the webserver - eg Apache - to do it. Django-staticfiles is for development only, and this is clearly stated throughout the documentation.