I uploaded a few icons to the static/img/ directory in my Django project, which is based on the Django Admin interface. I have one testing server and one production server. Both are using
https://fra1.digitaloceanspaces.com to store static project files. To display specific icons, I created custom field in Django ModelAdmin:
def get_type(self, obj):
return format_html(
'<img alt="type" src="{static_url}/img/type_icons/type-{type}.png">',
static_url=settings.STATIC_URL,
type=obj.type
)
project/settings.py
When developing locally STATIC_URL = '/collected-static/'.
In development and production mode STATIC_URL = '{}/{}/static/'.format(AWS_S3_ENDPOINT_URL, AWS_STORAGE_BUCKET_NAME).
AWS_S3_ENDPOINT_URL is https://fra1.digitaloceanspaces.com.
AWS_STORAGE_BUCKET_NAME is either project-develop (testing) or project (production). These variables are configured in deployment YAML file.
Locally and in development(testing) mode images are displayed perfectly fine this way, but inside production mode images are not displayed at all. I've tried changing the location of the files and even changing the deployment configuration, but nothing seems to help getting the icons to show up in production.
/collected-static/img/type-object_type.png (example of icon img src on localhost - OK)
https://fra1.digitaloceanspaces.com/project-develop/static/img/type-object_type.png (example of icon img src on testing server - OK)
https://fra1.digitaloceanspaces.com/project/static/img/type-object_type.png (example of icon img src in production - NOT DISPLAYING)
I would like to hear your opinion on this problem I am having. I've been struggling with this for quite some time. Perhaps there is a better way to handle static files in Django Admin.
checking the permission of the static directory on a production server
if you are using a web server like Nginx to serve the static directory will be useful
Related
[Update]
I've managed to upload a small file (but not yet a large image). ../media/filer_public/ sub-directories are being correctly created and file correctly uploaded. Need to investigate nginx configurations.
[OP]
I've logged into a new Django CMS system as superuser but cannot Add filer image or Add filer file to a page as the file upload silently fails; very briefly flashing its upload graphics but not actually uploading anything. I believe all the settings.py are correct as static artifacts are rendered correctly and Nginx has credible similar locations for both media and static directories.
I believe all file and directory permissions and ownerships are correct; i.e. that Nginx has user and / or group ownership of the Django CMS app directories and that permissions are correct.
The Postgres table filer_folder has a row for a new filer folder I created when editing a page but no corresponding directory has been created in the file system. I can add text and new text block plugins that get saved correctly.
Django CMS is running in a Docker container web which I have confirmed has rw (read/write) access to a Docker volume.
I see nothing abnormal in webs logs.
How can I find out what's (not) happening?
Simply adding client_max_body_size 10M; to the nginx configuration for the site solved the issue.
Similar issues were addressed in Stackoverflow and elsewhere:
Server Fault
Setting up Django and your web server with uWSGI and nginx
There are user avatars which are uploaded by user and stored in /media/users. In test environment these images are showing properly, but on production with identical code we have blank field instead of image and site.com/media/users/image_name.jpg redirects to other page, i. e. cannot find image. We cannot debug production server so I want to ask what possible causes can this behavior have?
is there a possibility that you are not serving the /media files using a webserver (like nginx) and the requests go to django app which (with the DEBUG flag turned off) doesn't handle media_url ?
I have some models with ImageFields (that work fine on my home computer test environment). When I put the project on a server using Passenger WSGI, submitting a form in the admin containing an ImageField doesn't work.
If DEBUG is True, I get a 404 error page saying that it can't find 500.shtml (I didn't create a 500 error page). No error appears in the server console.
If DEBUG is False, I get an Apache message saying it can't find "admin/red_projects/project/add/" (the URL it was JUST at), and the server error log has a message saying that the folder admin doesn't exist in the filesystem (since it's not a directory, just a Django urlconf), or it just hangs and doesn't load the page.
I already recursively set the permissions on the media directory to 755, and that didn't do anything. Everything else works fine, including submitting admin forms without ImageFields.
Django dosn't serve media or static files when debug is False, you shoud have and alias in your configuration to serve them via the web server directly (Way faster than using django to serve them)
Read this, it will help you put it in production : Deploying static files
Edit: This is for static files, same things aply to media files
I got into django recently and start playing around with the tutorials & documentation (with the development version). Everything has been fine till I decide to update again to the latest django trunk and well my admin media are not showing up at all!
After some troubles, I managed to get admin media showing by commenting out django.contrib.staticfiles. However as I do use the staticfiles app to manage my site static files, I need it to be enabled. After some troubles, I manage to get both admin media and staticfiles showing by using manage.py collectstatic to collect the admin media files to my static folder.
However is there a way for me to serve admin media in DEBUG mode easily like last time without using collect static command? as I don't want to call the collectstatic every time when django admin media files got changed in trunk? (though I don't know how often/rare that is)
Django trunk is changing how static files are served, and in fact new changes landed this morning.
You'll want to get latest again, and then start here: http://docs.djangoproject.com/en/dev/ref/contrib/staticfiles/
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.