【问题标题】:django application static files not working in productiondjango 应用程序静态文件在生产中不起作用
【发布时间】:2019-08-19 06:16:46
【问题描述】:

即使是管理页面,静态文件也无法在生产环境中运行。

我没有添加任何静态文件。

我的管理页面样式有问题。

我已按照以下教程创建 django 应用程序。

https://tutorial.djangogirls.org/en/

下面是我的设置.py

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/2.0/howto/deployment/checklist/

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

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

ALLOWED_HOSTS = ['127.0.0.1', '.pythonanywhere.com']


# Application definition

INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'blog', ]

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 = 'new_p.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 = 'new_p.wsgi.application'


# Database
# https://docs.djangoproject.com/en/2.0/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/2.0/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/2.0/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/2.0/howto/static-files/

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

我已经在生产环境中运行了 collectstatic。

(master)$ python3 manage.py collectstatic

You have requested to collect static files at the destination location as specified in your settings:

    /home/ag/ag.pythonanywhere.com/new_p/static

This will overwrite existing files! Are you sure you want to do this?

Type 'yes' to continue, or 'no' to cancel: yes

0 static files copied to '/home/agusm/agusm.pythonanywhere.com/new_p/static', 119 unmodified.

【问题讨论】:

  • 你用过collectstatic吗?您使用什么网络服务器来托管 django 项目?
  • 是的,我已经运行 collectstatic
  • 您是否将您的网络服务器配置为将/static 指向collectstatic 放置文件的文件夹?
  • Django 本身不提供文件;它将这项工作留给您选择的任何 Web 服务器。
  • 我现在还没有添加任何静态文件。我的管理页面静态文件没有加载

标签: python django production


【解决方案1】:

将这些文本粘贴到 settings.py 文件中

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

MEDIA_URL="static/media/"
MEDIA_ROOT=os.path.join(BASE_DIR,"static/media/")

然后将{% load static %}粘贴到您要使用静态文件或媒体的html文件中

然后将<img src="{% static 'media/images/mandeep.jpeg' %}" alt="My image"> 用于图像

对于 css 链接使用 <link rel="stylesheet" href="{% static '/css/style.css' %}">

有任何问题都可以问

【讨论】:

    【解决方案2】:

    我认为您的设置和运行collectstatic 的方式很好。

    如果您在 pythonanywhere 上进行托管,我假设您基于您的 ALLOWED_HOSTS,您需要关注 this guide

    设置静态文件映射

    最后,设置静态文件映射,让我们的网络服务器为您提供静态文件。

    转到 PythonAnywhere 仪表板上的 Web 选项卡 转到静态文件部分 在 url 部分输入与 STATIC_URL 相同的 URL(通常为 /static/) 在路径部分输入从STATIC_ROOT开始的路径(完整路径,包括/home/username/etc)

    然后点击重新加载并通过检索一个已知的静态文件来测试您的静态文件映射。

    例如,如果您在 /home/myusername/myproject/static/css/base.css 有文件,请访问 http://www.your-domain.com/static/css/base.css

    这基本上是您通过设置网络服务器(如pfitzer's answer 中的 nginx/apache)手动执行的操作,但我假设它正在执行通过 pythonanywhere 的网络界面设置目录别名的相同过程。

    【讨论】:

    • 我没有任何静态文件
    • 管理 css 文件是 Django 附带的静态文件。所以你确实有静态文件,只是不是你自己的。
    • Django 可以为我的管理员服务,只是没有静态文件:CSS、JS 等
    • 是的,我明白了。这些是静态文件,collectstatic 从 django 收集它们并将它们放在一个目录中,以便 Django 可以为它们提供服务。我不明白您还问什么,您是否尝试过上述方法?
    • 您设置的 pythonanywhere 静态映射是什么?设置这些静态映射后,您是否重新加载了您的 web 应用程序?
    【解决方案3】:

    如果你使用 nginx,你需要在你的配置中使用这个

    server{
    ...
        location /static/ {
            alias /path/to/static/;
            ...
        }
    ...
    }
    

    对于这样的apache

    <VirtualHost *:80>
    ...
    Alias /static "/path/to/static/"  
    <Directory "/path/to/static">  
        Order allow,deny
        Allow from all 
    </Directory>
    </VirtualHost>
    

    【讨论】:

    • autoindex on 不是必需的,实际上可能不需要。我建议删除它。
    【解决方案4】:

    Here is what you need

    当 collectstatic 运行时,默认的 STATICFILES_FINDERS 值 django.contrib.staticfiles.finders.FileSystemFinder 将从 STATICFILES_DIRS 中的任何路径收集您的静态文件。

    另一个默认的 STATICFILES_FINDERS 值 django.contrib.staticfiles.finders.AppDirectoriesFinder 将在您的 INSTALLED_APPS 中任何应用的 /static/ 文件夹中查找。

    找到的所有静态文件都会放在指定的STATIC_ROOT目录下。

    $ python manage.py collectstatic
    

    这会将静态文件夹中的所有文件复制到 STATIC_ROOT 目录中。

    你也可以使用

    python manage.py findstatic
    

    查看 collectstatic 将查看哪些目录。

    【讨论】:

      【解决方案5】:

      将静态文件投入生产的基本大纲很简​​单,运行 collectstatic 命令

      您需要配置您的网络服务器以提供 STATIC_ROOT 中 URL STATIC_URL

      下的文件

      参考这个link

      【讨论】:

        猜你喜欢
        • 2015-11-26
        • 1970-01-01
        • 2019-12-10
        • 2012-01-14
        • 2016-08-20
        • 2012-08-16
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多