Reputation: 363
Django is not serving my media files when DEBUG = False
.
Here is my code:
DEBUG = False
STATIC_URL = '/static/'
STATICFILES_DIRS = [
os.path.join(BASE_DIR, "static"),
]
STATIC_ROOT = os.path.join(BASE_DIR, "staticfiles")
MEDIA_URL = '/media/'
MEDIA_ROOT = os.path.join(BASE_DIR, 'static', 'media')
I've tried several things but none have worked.
Please help me if you know the answer to my problem!
Upvotes: 1
Views: 3718
Reputation: 150483
The Django documentation includes help about this (link):
Serving files uploaded by a user during development
During development, you can serve user-uploaded media files from
MEDIA_ROOT
using thedjango.views.static.serve()
view.This is not suitable for production use! For some common deployment strategies, see How to deploy static files.
For example, if your
MEDIA_URL
is defined asmedia/
, you can do this by adding the following snippet to yourROOT_URLCONF
:from django.conf import settings from django.conf.urls.static import static urlpatterns = [ # ... the rest of your URLconf goes here ... ] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
Note
This helper function works only in debug mode and only if the given prefix is local (e.g. media/) and not a URL (e.g. http://media.example.com/).
I think the step that you are missing is that you haven't modified your root URLs to point MEDIA_URL
URLs to files found in MEDIA_ROOT
.
Upvotes: 0
Reputation: 5055
As i can see your question is regarding media files but the answers are given about static files which are different if we see them at deployment level. For static files, you can simply use whitenoise. That will work perfectly. It will also work with media files but only when DEBUG = True and for shorter time(e.g, if you deploy your app on some platform which uses dynos like heroku, dynos refresh after a particular time. In heroku it is 30 min and after that media files will be deleted no matter whether the debug is set to true or false.) Which is not secure during deployment.So to make your media files working, you need some third party support to serve your media files to your site like Amazon S3 buckets which will store your media files and will serve them to your website. Hope you got the answer!
Upvotes: 0
Reputation: 188
if you want to deploy your django project best practices please set DEBUG=False
and ALLOWED_HOSTS=['your_host']
and than collect your static and media files using command ./manage.py collectstatic
Upvotes: 0
Reputation: 2826
I recently had a similar issue.
In development, the Django development server serves the static files as documented in the Django docs. If settings.DEBUG
is False
then static file serving by the development server stops. See this Stack Overflow post: Why does DEBUG=False setting make my django Static Files Access fail?
In addition to these configuration steps, you’ll also need to actually serve the static files.
During development, if you use django.contrib.staticfiles, this will be done automatically by runserver when DEBUG is set to True (see django.contrib.staticfiles.views.serve()).
This method is grossly inefficient and probably insecure, so it is unsuitable for production.
For production, there are options for serving static files as documented here.
I solved the issue of serving static files in production on an instance of Dokku—the smallest PaaS implementation you've ever seen—that is similar to Heroku.
I used a package called Whitenoise that is discussed in this blog post.
I found Whitenoise to be the easiest solution to serving static files for a small Django application without having to configure a separate static file server.
Upvotes: 1