14

I currently have two kind of files static files and media files.The static files contain my css,js and other static content. The media files contain stuff that the user uploads.The static folder is right next to the media folder.Now on my deployed machine. If I set the DEBUG = False my static files are presented just fine however my media content is never displayed. I get a 404 not found error. My quuestion is how do I display media content in production environment when DEBUG = False. With DEBUG= True everything seems to work fine ? I read the following page and tried these things out.

1-Tried creating a separate folder for static media just like static content. Currently my static content is being managed by the collectstatic command. I am using apache on webfaction.I have a static folder called static_content when I ran ./manage.py collectstatic all of my content in static folder was copied to the static_content folder.I tried creating another static folder called static_media. However when I ran ./manage.py collectstatic the content of my media folder got copied to static_content and not to static_media like it should have. Can anyone tell me why the collectstatic command did not paste the content to static_media instead ?

This is what my configuration looks like

ALLOWED_HOSTS = [
   "*",
   'mywebsite.com',
   'www.mywebsite.com.com',
]


STATIC_URL = 'http://mywebsite.com.com/static/'
STATIC_ROOT = '/home/admin/webapps/static_content'
STATICFILES_DIRS = (
                        '/home/admin/webapps/mainfolder/mainapp/static',
                        '/home/admin/webapps/mainfolder/mainapp/media',
                   )
PROJECT_PATH = os.path.join(BASE_DIR, 'static')

MEDIA_URL = 'http://mywebsite.com.com/media/'
MEDIA_ROOT = '/home/admin/webapps/static_media' 

This is what my urls.py looks like

admin.autodiscover()
urlpatterns = [
....
]+ static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)

Am I using the correct approach creating a separate folder for managing my media content just like for my static content for when DEBUG=False ? If so then why is collectstatic command dumping all my media content into my static_content folder instead of static_media folder ? Also if I use a static folder for managing my media content when a user uploads data where will that be uploaded to media or static_media.

MistyD
  • 16,373
  • 40
  • 138
  • 240

10 Answers10

26

In your urls.py file:

add this line

from django.views.static import serve

add those two urls in urlpatterns:

url(r'^media/(?P<path>.*)$', serve,{'document_root': settings.MEDIA_ROOT}),
url(r'^static/(?P<path>.*)$', serve,{'document_root': settings.STATIC_ROOT}),

It worked for me :)

Axnyff
  • 9,213
  • 4
  • 33
  • 37
stathoula
  • 1,520
  • 2
  • 11
  • 11
  • 8
    Shouldnt be used in production though: https://docs.djangoproject.com/en/2.2/ref/views/#serving-files-in-development – Ron Nov 14 '19 at 08:25
11

You shouldn't use collectstatic for your media directory. Remove '/home/admin/webapps/mainfolder/mainapp/media' from STATICFILES_DIRS, then set

MEDIA_ROOT = '/home/admin/webapps/mainfolder/mainapp/media'

Once you've done this, the static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT) should serve media files when DEBUG = True.

For DEBUG = False, you have to configure Apache to serve the media files.

Alasdair
  • 298,606
  • 55
  • 578
  • 516
  • I already have the media files working for DEBUG = True. I guess Ill have to configure apache. – MistyD Jun 14 '17 at 22:32
  • Didn't sound like it was working in your question - "If I set the DEBUG = True my static files are presented just fine however my media content is never displayed" – Alasdair Jun 14 '17 at 23:16
  • Ah thanks for catching that. I just fixed it. I am trying to figure out how I can configure a httd.conf I cant find apache2,conf in my deployment server – MistyD Jun 14 '17 at 23:39
  • 1
    The location of your apache config depends on the operating system (and perhaps your hosting provider if you are using shared hosting). That's really a separate question, so I can't help you with that. – Alasdair Jun 15 '17 at 09:02
9

If you are using Nginx, let it to serve media files

For Example

go to nginx/sites-available & add this

location /media/ { root */home/myprojectdir/myproject*; } 
Antu
  • 2,197
  • 3
  • 25
  • 40
er23
  • 101
  • 1
  • 2
6

I have faced the same issue with DEBUG=False. I solved it by configuring Nginx media location in the server block like this

location /media/ {
    root FULL_PATH_TO_APP; 
}

FULL_PATH_TO_APP is the full path to the directory where my media folder exists.

Mahbubur Rahman
  • 361
  • 2
  • 8
3

In Pythonanywhere server Just add Static file url and Directory

URL
/static/
/media/

Directory path

/home/taukir707/myblog/static

/home/taukir707/myblog/media

0

This is the best solution. Keep you media folder inside your static folder. And use this code in

Settings.py

# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/3.1/howto/static-files/
if(DEBUG==True):
    STATIC_URL = '/static/'
    MEDIA_URL = '/static/media/'
    STATICFILES_DIRS=[
         os.path.join(BASE_DIR,'static')
    ]
    STATIC_ROOT='/home/username/websitedomain/static'
    MEDIA_ROOT='/home/username/websitedomain/static/media'
else:
    STATIC_URL = '/static/'
    MEDIA_URL='static/media/'
    STATIC_ROOT=os.path.join(BASE_DIR,'static')
    MEDIA_ROOT=os.path.join(BASE_DIR,'static/media/')
REVANTH N D
  • 167
  • 3
  • 11
0

this helps me a lot

settings.py

STATIC_URL = '/static/'


STATIC_ROOT = os.path.join(BASE_DIR, "static/")


MEDIA_URL =  '/media/'


MEDIA_ROOT = os.path.join(BASE_DIR, "media")


STATICFILES_STORAGE = 'whitenoise.storage.CompressedStaticFilesStorage'


INSTALLED_APPS = [
 
    'django.contrib.staticfiles',

    'whitenoise.runserver_nostatic',

]

in url.py import URL and give static and media file URL

from django.views.static import serve

from django.conf.urls import url
Usman Maqbool
  • 3,351
  • 10
  • 31
  • 48
  • As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Feb 06 '22 at 12:07
0
  1. In settings.py: DEBUG = False

  2. In urls.py :

if not settings.DEBUG:
    urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
  1. $> python manage.py runserver

Work fine in deployment or development!

Lucas Grugru
  • 1,664
  • 1
  • 4
  • 18
0

devserver in secure mode

Step 1

Define a STATIC_ROOT and MEDIA_ROOT path in settings.py.

Code: settings.py

            STATIC_URL = '/static/'

            MEDIA_URL = '/media/'



            if DEBUG:

                  STATICFILES_DIRS = [os.path.join(BASE_DIR, 'static')]

            else:

                  STATIC_ROOT = os.path.join(BASE_DIR, 'static')

            MEDIA_ROOT = os.path.join(BASE_DIR, 'media')

Step 2

Code: urls.py

            from django.conf.urls import url
            from django.conf import settings
            from django.views.static import serve

            urlpatterns = [
                  url(r'^media/(?P<path>.*)$', serve,{'document_root': settings.MEDIA_ROOT}),
                  url(r'^static/(?P<path>.*)$', serve,{'document_root': settings.STATIC_ROOT}),
            ]
            

Then run py manage.py runserver

0

Need to change Ngingx to add location:

location /media/ {
   alias /home/xuser/xproject/media/;
   expires 1d;
}
Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
  • Please don't post only code as answer, but also provide an explanation what your code does and how it solves the problem of the question. Answers with an explanation are usually more helpful and of better quality, and are more likely to attract upvotes. – Mark Rotteveel Feb 19 '23 at 09:32