Django media files not showing with Debug = False on production - Django 1.10

孤街浪徒 提交于 2021-01-02 05:46:11

问题


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.


回答1:


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.




回答2:


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 :)




回答3:


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*; } 



回答4:


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.




回答5:


In Pythonanywhere server Just add Static file url and Directory

URL
/static/
/media/

Directory path

/home/taukir707/myblog/static

/home/taukir707/myblog/media



来源:https://stackoverflow.com/questions/44555187/django-media-files-not-showing-with-debug-false-on-production-django-1-10

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!