在Microsoft Azure中生产后,Django静态文件给出404错误

时间:2018-07-31 12:29:16

标签: django azure django-admin django-staticfiles

我的settings.py包含以下代码:

BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))


DEBUG = False

STATIC_URL = '/static/'

STATIC_ROOT = 'D:/home/site/wwwroot/static'

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

将代码推送到github后,我将github代码部署到Azure中,然后在Azure资源的Web控制台中运行“ manage.py collectstatic --noinput”,并且当我运行项目url时,静态文件仍然显示404错误找不到,即使admin css也给出了相同的错误,尽管静态文件夹具有admin css。

有人可以帮我弄清楚我可能是什么错误。

2 个答案:

答案 0 :(得分:0)

似乎当DEBUG = False时,未定义STATIC_ROOT的静态文件URL,因此必须在url.py中显式定义一个指向STATIC_ROOT静态文件夹的URL。

我通过将settings.py中的以下代码更改为:

STATIC_URL = '/static/'

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

STATICFILES_DIRS = [
    'D:/home/site/wwwroot/static',
    os.path.join(BASE_DIR,'static'),
]

然后我将以下内容添加到我的urls.py中:

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

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

答案 1 :(得分:0)

我遇到了同样的问题,但是幸运的是,我能够根据以上答案解决它。但是,它需要稍作更改。 django == 3.0.8的工作解决方案

settings.py确保像这样:

O(1)

urls.py需要像这样:

STATIC_URL = '/static/'
STATIC_ROOT= os.path.join(BASE_DIR,'static')
STATICFILES_DIRS = [
    'D:/home/site/wwwroot/static',
    os.path.join(BASE_DIR,'static'),
]