找不到:/ media / 404 77 Django生产-ASGI Digital Ocean [英] Not Found: /media/ 404 77 Django production - ASGI Digital Ocean

查看:223
本文介绍了找不到:/ media / 404 77 Django生产-ASGI Digital Ocean的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经尝试了所有方法,但是我的 media 文件夹个人资料图片仍然没有出现。他们在网页上看起来像这样。

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

我正在生产Django 2.1应用,使用运行ASGI服务器的Digital Ocean(因为我正在使用渠道)。

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

我的 media 文件夹位于我的根文件夹中(与 manage.py相同级别) )。

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

给出的错误是

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.

我的settings.py

my settings.py

ASGI_APPLICATION ='sobr.routing.application'

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上游

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

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

回溯

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 -


推荐答案

您是否正确设置了服务器?请检查您的nginx上游媒体文件,如下所示。

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/;
    }
.........



}

这篇关于找不到:/ media / 404 77 Django生产-ASGI Digital Ocean的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆