Not Found: /media/ 404 77 Django production - ASGI Digital Ocean

你。 提交于 2019-12-25 01:36:04

问题


I've tried everything but my media folder profile pic images still aren't appearing. They look like this on the webpage.

I'm in production for my Django 2.1 app, using Digital Ocean running a ASGI server (as I'm using channels).

My media folder is located in my root folder (same level as manage.py).

The error being given is

xx.xxx.xxx.xx:xxxxx - - [23/Feb/2019:17:23:49] "GET /media/profile_pics/avril.jpg" 404 77

But that is the correct path and the image is located there. All of my static files are rendering fine.

my settings.py

ASGI_APPLICATION = 'sobr.routing.application'

TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [],
        'APP_DIRS': True,
        'OPTIONS': {
            'context_processors': [
                'django.template.context_processors.debug',
                'django.template.context_processors.request',
                'django.contrib.auth.context_processors.auth',
                'django.contrib.messages.context_processors.messages',
            ],
        },
    },
]

STATIC_URL = '/static/'

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

STATICFILES_DIRS = (
    os.path.join(BASE_DIR, 'appname/static'),
)

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

MEDIA_URL = '/media/'

CHANNEL_LAYERS = {
    "default": {
        "BACKEND": "channels_redis.core.RedisChannelLayer",
        "CONFIG": {
            "hosts": [os.environ.get('REDIS_URL', 'redis://xx.xxx.xxx.xx:8080')],
        },
    },
}

urls.py

if settings.DEBUG:
    urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
    urlpatterns += static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)

nginx upstream

   location /static/ {
        root /home/user/appname/src/appname;
    }

    location /media/  {
        alias /home/user/appname/src/appname/media;
    }

traceback

2019-02-24 18:42:02,932 WARNING  Not Found: /media/profile_pics/girl.jpg
2019-02-24 18:42:02,934 WARNING  Not Found: /media/profile_pics/avril.jpg
2019-02-24 18:42:02,933 WARNING  Not Found: /media/profile_pics/man.jpg
xx.141.198.14:59401 - - [24/Feb/2019:18:42:03] "GET /media/profile_pics/brad_2G59aPW.jpg" 404 77
xx.141.198.14:59400 - - [24/Feb/2019:18:42:03] "GET /media/profile_pics/girl.jpg" 404 77
xx.141.198.14:59402 - - [24/Feb/2019:18:42:03] "GET /media/profile_pics/avril.jpg" 404 77
xx.141.198.14:59399 - - [24/Feb/2019:18:42:03] "GET /media/profile_pics/man.jpg" 404 77
xx.141.198.14:59404 - - [24/Feb/2019:18:42:03] "GET /static/fonts/PermanentMarker-Regular.ttf" 304 -
xx.141.198.14:59405 - - [24/Feb/2019:18:42:03] "GET /static/fonts/SourceSansPro-Light.ttf" 304 -
xx.141.198.14:59401 - - [24/Feb/2019:18:42:03] "GET /static/fonts/Poppins-SemiBold.ttf" 304 -

回答1:


Have you setup server properly? Please check your nginx upstream for media file as below.

server {
    listen 80;

    ............

    location /static/ {
        alias /home/project/staticfiles/;
    }
    location /media/ {
            alias /home/project/media/;
    }
.........



}


来源:https://stackoverflow.com/questions/54844183/not-found-media-404-77-django-production-asgi-digital-ocean

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