静态文件在生产中不可见| DJANGO

问题描述 投票:0回答:2

我有点困惑。 我在项目中使用django-el-pagination应用程序。 此应用在模板中使用下一个javascript:

<script src="{% static 'el-pagination/js/el-pagination.js'%}"></script>

在django开发服务器中工作完美,但在生产中显示404。

在生产中,我安装了此应用程序。 在我的settings.py文件下面:

DEBUG = True

MIDDLEWARE = [
    'django.middleware.security.SecurityMiddleware',
    'django.contrib.sessions.middleware.SessionMiddleware',
    'django.middleware.common.CommonMiddleware',
    'django.middleware.csrf.CsrfViewMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'django.contrib.messages.middleware.MessageMiddleware',
    'django.middleware.clickjacking.XFrameOptionsMiddleware',
]

TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [
            os.path.join(BASE_DIR, 'templates'),
        ],
        'APP_DIRS': True,
        'OPTIONS': {
            'context_processors': [
                'django.template.context_processors.debug',
                'django.template.context_processors.request',
                'django.contrib.auth.context_processors.auth',
                'django.template.context_processors.tz',
                'django.contrib.messages.context_processors.messages',
            ],
        },
    },
]

STATICFILES_DIRS = [os.path.join(BASE_DIR, 'static')]
STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR, 'static_root')

我也做了python manage.py collectstatic命令。 我忘了做什么?

python django python-2.7 django-1.11
2个回答
2
投票

文档中

在开发过程中,如果使用django.contrib.staticfiles,则当DEBUG设置为True时,将由runserver自动完成(请参阅django.contrib.staticfiles.views.serve())。

因此,如果您不使用runserver并且DEBUG设置为False则不应提供这些文件。

无论如何,如果您也从同一文档中考虑到这一点:

该方法效率极低并且可能不安全,因此不适合生产。

以下是在生产环境中提供静态文件的一些建议方法 ,例如,链接的一种方法是与反向代理服务器(例如nginx或apache)一起提供静态文件。


-1
投票

生产服务器通常会发生这种情况。 您可以从生产服务器访问静态文件

<script src="https://techoryze.herokuapp.com/static/js/jquery.js" />

检查截图

© www.soinside.com 2019 - 2024. All rights reserved.