Serve media files with a static service. Dotcloud - django

I'm trying to serve the media and static files of my django app with a static service on dotcloud (http://stackoverflow.com/questions/13681183/serve-static-files-with-nginx-and-custom-service-dotcloud).
I created a static service where I put my static files. In my django settings, I have:
STATIC_ROOT = STATIC_DIR
STATIC_URL = 'http://mystaticapp.dotcloud.com/'
It works perfectly.
Concerning my media files, I put a folder media in my static service, and I have:
MEDIA_ROOT = 'http://mystaticapp.dotcloud.com/media/'
MEDIA_URL = 'http://mystaticapp.dotcloud.com/media/'
MEDIA_URL works find: my django app get the media from the static service.
But the problem happens with MEDIA_ROOT. It looks like the app is not uploading the files to my static app. When I upload profile picture for my users, it does not upload it to mystaticapp/media/profilepic, as it should.
Any idea on how I could solve that problem? Thank you.

The important thing to remember is that the static service and your django service on dotCloud are separate services, that have no common file system. So if you upload a file to your django service, it won't be available to your static service.
For this use case I would recommend uploading your files to S3, and serving those files from there.
I would recommend Django-storages for this. Here is some documentation: http://django-storages.readthedocs.org/en/latest/backends/amazon-S3.html

Related

Django Static Files not Loading on Heroku, GCS deployment

So, I have created a django application and deployed it with Heroku. Static files (as well as media) are served by a GCS bucket.
I am currently having a problem loading all of my static files. Notably, the png files are not loading, but everything else is. The png file requests return a 403 error, but the rest of the files (css, js, etc.) load fine.
Furthermore, jpg images that were uploaded via media are returned without error.
I have to assume that this is a GCS problem, but I'm not entirely sure of that. I have tried everything in including assigning permissions to my service agent, creating another service agent, and recreating another bucket with the same data.
I have not had this problem when using GCS buckets with applications running on Google App Engine or GKE.
Any point in the right direction would be great.
This is the relevant part of my settings file. Note that I put the django_heroku.settings(locals()) call above this block of code intentionally, because it overrides these settings.
DEBUG = False
django_heroku.settings(locals())
DEFAULT_FILE_STORAGE = 'storages.backends.gcloud.GoogleCloudStorage'
STATICFILES_STORAGE = 'storages.backends.gcloud.GoogleCloudStorage'
GS_BUCKET_NAME = 'hello-world'
GCS_URL = 'https://storage.googleapis.com/'
STATIC_URL = '{}/{}/static/'.format(GCS_URL, GS_BUCKET_NAME)
MEDIA_URL = '{}/{}/media/'.format(GCS_URL, GS_BUCKET_NAME)
STATIC_ROOT = os.path.join(BASE_DIR, 'static/')
MEDIA_ROOT = os.path.join(BASE_DIR, 'media/')
UPLOAD_ROOT = 'media/uploads/'
I figured it out. The files that were missing were being called from the CSS file (background images, etc). Since they weren't being called with the {% static %} tag, the appropriate signatures weren't being added onto the call, and the bucket was seeing an unauthorized attempt to access the files.
My solution was to move these calls to bucket files inline, so that I could use the static tag.

django wont serve media from heroku

okay so am using whitenoise to store my static files, thats not an issue when deploying to heroku that works perfectly well.
So I need to use S3 to serve my media files. I can upload perfectly to the bucket with no issues, from the website interface that is, but when it tries to load the actual images am trying to serve I instantly get a 404, when I check this 404 in the dev console it shows me the debug message and the link, when I visit the link it actually exists.
So my amazon credentials work, so does CORS and bucket policy, even accessing the images through the link that it claims does exist works.
This is also my MEDIA_ROOT
MEDIA_ROOT = 'https://%s/' % AWS_S3_CUSTOM_DOMAIN
and prints out correctly, is it an issue with using whitenoise and boto storage together?
edit:
so now my static files serve perfectly from S3 but not my media. seems to be ignoring my media_root and media_url

Django storages azurebackend CMS not displaying media files

I'm using the storages and django-cms apps with the azure-storage backend in my Django site. The storage is working great, as when I upload files they are uploaded to my blob container perfectly. I can confirm that the files are there and publicly accessible. The problem lies in django-cms display of the images. The img src is blank and the images are not displayed at all. I've changed both MEDIA_ROOT, MEDIA_URL to my blob storage location but still nothing. Any ideas?
How are you confirming that the files are there and publicly accessible? Are you confirming through the management portal?
I don't know much about Django but I skimmed over this page https://docs.djangoproject.com/en/1.8/topics/files/. From what I understand, MEDIA_ROOT and MEDIA_URL are used for locally stored files. I'm not sure if an Azure Storage endpoint can be used here but again I'm not familiar with Django.

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

Django and Serving Static Files

I'm hosting a site on WebFaction using Django/mod_python/Python2.5. I've recently run into the concept of static files (when setting up my Django admin).
From what I understand, serving static files is simply the idea of telling the server to serve files directly from a specific directory, rather than first routing the request through apache, then mod_python, then django, and finally back to the user. In the case of WebFaction this helps especially since there are two Apache servers that your request must go through (your app's server and the main public server).
Why is it that when I setup Django's static files, it only needs the /media folder in /contrib/admin? Is it just that all of Django's static content is related to the admin panel?
When I want to serve my own static content (images, css, etc.) should I include it in the same /media folder or set up another alias for my own content (/my_media)?
Yes, the static files used by Django are pretty much related to images, javascript and css for the admin. All other static content comes from your application. You can keep both sets (yours and the admin) under the same server. Just set the appropriate folders in the settings file.
http://docs.djangoproject.com/en/dev/ref/settings/#admin-media-prefix
http://docs.djangoproject.com/en/dev/ref/settings/#media-root
http://docs.djangoproject.com/en/dev/ref/settings/#media-url
See this post for a little more information:
Django and Static Files
Django's static files (e.g. js, css, images, etc.) are all in the media folder, and are related to the admin panel.
On WebFaction to save processing power, and more importantly memory, it is better to serve these from your secondary apache server (or even better from nginx or lighttpd) without having to go through mod_python and Django.
I use the following folder setup for my files:
media
css
js
img
etc
admin
css
js
img
See http://forum.webfaction.com/viewtopic.php?id=1981 for how to setup nginx as your secondary server on WebFaction if you are interested.