python - Django,即使在 collectstatic 之后也没有提供 uwsgi 静态文件

标签 python django django-staticfiles

我在 Debian 8 VPS 上部署 Django 应用程序时遇到问题。 Python 版本 2.7,Django 1.10.2。

我的问题是即使在运行“collectstatic”并分配 STATIC_ROOT 目录后,它也不会在生产模式下提供静态文件(DEBUG = False)。

我已按照有关此部署(nginx、uwsgi、python)的每条说明进行操作,但我的所有静态文件仍然收到 404。当运行 collectstatic 时,它会将所有文件放入应用程序顶部的/static/目录中。当我运行 uwsgi 或开发服务器时,HTML 和 python 功能正常,但我所有的静态 CSS、JS、IMG 都无法访问。

当我切换回 DEBUG=True 并运行开发服务器时,我的静态文件再次出现。

有人可以看看我做错了什么吗?如果您需要更多文件上下文,请告诉我。

我的settings.py内容如下:

"""
Django settings for mysite project.

Generated by 'django-admin startproject' using Django 1.10.2.

For more information on this file, see
https://docs.djangoproject.com/en/1.10/topics/settings/

For the full list of settings and their values, see
https://docs.djangoproject.com/en/1.10/ref/settings/
"""

import os
# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))


# Quick-start development settings - unsuitable for production
# See https://docs.djangoproject.com/en/1.10/howto/deployment/checklist/

# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = 'removedforstackoverflow'

# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = False

ALLOWED_HOSTS = ['removedforstackoverflow']


# Application definition

INSTALLED_APPS = [
    'main',
    'instant',
    'opengig',
    'widget_tweaks',
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
]

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',
]

ROOT_URLCONF = 'mysite.urls'

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',
            ],
        },
    },
]

WSGI_APPLICATION = 'mysite.wsgi.application'


# Database
# https://docs.djangoproject.com/en/1.10/ref/settings/#databases

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.sqlite3',
        'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
    }
}


# Password validation
# https://docs.djangoproject.com/en/1.10/ref/settings/#auth-password-validators

AUTH_PASSWORD_VALIDATORS = [
    {
        'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator',
    },
    {
        'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator',
    },
    {
        'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator',
    },
    {
        'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator',
    },
]


# Internationalization
# https://docs.djangoproject.com/en/1.10/topics/i18n/

LANGUAGE_CODE = 'en-us'

TIME_ZONE = 'UTC'

USE_I18N = True

USE_L10N = True

USE_TZ = True


# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/1.10/howto/static-files/

STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR, "static/")

这是我调用静态文件的 header.html 文件。如果它只是 Bootstrap ,我会使用 CDN,但我有一些我使用的图像,当这个应用程序很小的时候,我宁愿不设置一个服务器来托管我的静态文件。

<!DOCTYPE html>
<html lang="en">
<head>
    <title>Instant Backoffice</title>
    <meta charset="utf-8" />
    {% load staticfiles %}
    <link rel="stylesheet" href="{% static 'css/bootstrap.min.css' %}" type = "text/css"/>
    <meta name="viewport" content = "width=device-width, initial-scale=1.0">
</head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
<script type="text/javascript" src="{% static 'js/bootstrap.min.js' %}"></script>
<body>
    <nav class="navbar navbar-default">
        <div class="container-fluid">
            <div class="navbar-header">
                <a class="navbar-brand" href="/">
                    <img alt="Brand" src="{% static 'img/logo.svg' %}" height="25">
                </a>
            </div>
            <ul class="nav navbar-nav navbar-right">
                <ul class="nav nav-pills">
                    <li><a href="/instant/allorders">List of Orders</a></li>
                    <li><a href="/instant/payment">Change Payment Status</a></li>
                    <li><a href="/instant/review">Add a Review</a></li>
                    <li><a href="/instant/cancel">Cancel an Order</a></li>
                    <li><a href="/logout/">Logout</a></li>
                </ul>
            </li>
        </ul>
    </div>
</nav>
<div class="row">
    <div class='container-fluid'>
        <div class="col-sm-12">
            {% block content %}
            {% endblock %}
        </div>
    </div>
    </div>
</body>
</html>

来自开发服务器的错误列表:

[06/Oct/2016 23:40:43] "GET /static/css/bootstrap.min.css HTTP/1.1" 404 102
[06/Oct/2016 23:40:43] "GET /static/js/bootstrap.min.js HTTP/1.1" 404 100
[06/Oct/2016 23:40:44] "GET /static/img/logo.svg HTTP/1.1" 404 93
[06/Oct/2016 23:40:44] "GET /static/js/bootstrap.min.js HTTP/1.1" 404 100
[06/Oct/2016 23:40:44] "GET /static/img/logo.svg HTTP/1.1" 404 93
[06/Oct/2016 23:40:44] "GET /static/img/bg.jpg HTTP/1.1" 404 91

正如我所说,我刚收到 404,但我不明白为什么。任何帮助都会很棒。

最佳答案

您需要确保项目的 urls.py 文件已更新以提供来自生产环境的静态文件。

使用以下代码更新项目的 urls.py 文件,如下所示。

from django.conf import settings
from django.conf.urls.static import static

urlpatterns = [
    # ... the rest of your URLconf goes here ...
] + static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)

另一件事,我也没有在您的 settings.py 文件中看到任何 STATICFILES_DIRS 目录。你也需要更新它,这样当你运行 ./manage.py collectstatic 命令时,静态目录中的所有静态 Assets 都会被收集到你的根 static 目录中,即是 Django 中的最佳实践,即使直接将静态文件放在 STATIC_ROOT 目录中也适用于生产。

关于python - Django,即使在 collectstatic 之后也没有提供 uwsgi 静态文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39907281/

相关文章:

python - 需要在 python 中获取迭代器的其余部分

python - 使用 plt.legend 编辑时 Seaborn 散点图图例中断

python - django过滤包含整数的模型

django - 如何在 Django REST Framework 中返回自定义 JSON 输出

Python 开发环境

django - 为什么 DEBUG=False 设置会使我的 django 静态文件访问失败?

python - 将数据帧向量添加到数据帧表

python - 如何在 Python 中比较两个 SQLite 数据集并搜索相似之处?

javascript - 在 Django 中需要上下文变量的 Javascript 最佳实践

python - 在django中加载静态文件