Perhaps a misconception on my part, I don't understand what is the purpose of MEDIA_URL and MEDIA_ROOT when using S3. The files are uploaded to the bucket so the root is never used, and the url of the ImageField is stored inside the image object so the MEDIA_URL is never used either. Is this correct? If not, please explain it so I can better understand how everything fits together.
My setup has three buckets:
static for js, css, templates, set as STATICFILES_STORAGE
media for user uploaded images, set as DEFAULT_FILE_STORAGE
spider for images scraped from other websites.
Given that you are using django-storages, you can safely remove these two variables from your settings.py file.
Both variables are used for when you are handling media files on the server itself.
Related
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.
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
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.
Basically these are my settings.py settings;
MEDIA_ROOT = 'C:/Users/Deep.C/.virtualenvs/projectcontainer/mysite/media/'
MEDIA_URL = '/media/'
CKEDITOR_UPLOAD_PATH = MEDIA_ROOT
Image upload is working within the Rich Text Editor, as the file is turning up in the correct directory.
However when trying to browse images the images are never being loaded due from I assume is the path to the image being incorrect as follows;
<img src="/media/2013\06\25\Google-Chrome-icon_thumb.png">
It is returning a 404 NOT FOUND. In the console the path being displayed http://localhost:8080/media/2013/06/25/Google-Chrome-icon_thumb.png
I know this is a fairly trivial thing probably me not setting things up correctly but i cant seem to figure out where i could have gone wrong.
Any help would be appreciated!
Thanks
Static files and files served from media are done through 2 different methods. In order for the media files to be served you need to serve files from the media folder. To do so follow the guide at https://docs.djangoproject.com/en/dev/howto/static-files/
The section you are interested in is "Serving files uploaded by a user".
I'm confused about static files and media files in django. I've seen elsewhere that people use it interchangeably.
When should I use media_root and when should I use static_root?
If I have site images should I put it in static? And if I have product images do I put it in media?
MEDIA_ROOT is the directory where file uploads are placed, as well as where generated files are usually stored. For example, one of my Django apps allows users to upload images. In one of the model classes, I use the ImageField type from sorl-thumbnail with upload_to='%Y-%m'. Whenever a user uploads an image, the file is stored in MEDIA_ROOT/%Y-%m/ (with %Y replaced with the current year and %m replaced with the current month number). Also, when sorl-thumbnail generates a thumbnail for an uploaded image, it places the thumbnail by default somewhere in MEDIA_ROOT/cache/.
STATIC_ROOT is used to configure the directory where static assets are placed. For example, site stylesheets, JavaScript files, and images used in the design of web pages are the types of files that go into STATIC_ROOT. If you have multiple installed apps, each app that uses static files can have its own static files directory. You use the collectstatic management function (invoked via python manage.py collectstatic) to copy all of the apps' static files into STATIC_ROOT.