当源中有其他静态文件时,为什么不能在django项目中添加静态文件? [英] Why can't I add static files in my django project when other static files are available in sources?

查看:70
本文介绍了当源中有其他静态文件时,为什么不能在django项目中添加静态文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试为Heroku构建Django应用,并通过了民意调查教程和 Heroku文档

I'm trying to build a Django app for Heroku and have gone through the Polls tutorial and Heroku documentation

当使用heroku localpython3 manage.py runserver提供基本的Django-heroku应用程序时,我可以看到一些静态文件已经很好地加载了(特别是/static/lang-logo.png).但是,当我添加main.css并运行python3 manage.py collectstatic时,css文件将不会显示,并且index.html中的导入也将不会加载.

When serving the basic Django-heroku app with heroku local or python3 manage.py runserver I can see that some static files are loaded just fine (/static/lang-logo.png specifically). But when I add my main.css and run python3 manage.py collectstatic the css file never shows up and the import in my index.html never loads.

我已经准备好白噪声最适合在Django应用中提供静态文件,因此我正在使用它,并根据对此处类似问题的许多回答设置了settings.py.

I've ready that whitenoise is best for serving static files in a django app so I'm using that and have set my settings.py based on many of the responses to similar questions here.

Collectstatic似乎正在运行(请参阅下文).从字面上看,我一直在努力应对这一问题,并且在这里经历了数十个相关问题,而且似乎没有任何解决方法.我已经到了互联网的尽头,现在你是我唯一的希望.我的代码在下面,如果我意识到我错过了一些相关的东西,我将更新问题.

Collectstatic appears to be working (see below). I've been banging my head against this for literally days and have gone through dozens of related questions here and nothing seems to cut it. I have reached the end of the internet and now you are my only hope. My code is below and I'll update the question if I realize I've missed something relevant.

Settings.py

"""
Django settings for gettingstarted project.

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

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

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

import os
import django_heroku
import fred

# 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 = 'CHANGE_ME!!!! (P.S. the SECRET_KEY environment variable will be used, if set, instead).'

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

ALLOWED_HOSTS = []


# Application definition

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

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',
    'whitenoise.middleware.WhiteNoiseMiddleware',
]

ROOT_URLCONF = 'gettingstarted.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 = 'gettingstarted.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_ROOT = os.path.join(BASE_DIR, 'staticfiles')
STATIC_URL = 'hello/static/hello'
STATICFILES_DIRS = (
    os.path.join(BASE_DIR, 'hello/static/hello'),
)

django_heroku.settings(locals())

index.html

{% load staticfiles %}
{% load static %}

<link rel="stylesheet" type="text/css" href="{% static 'hello/screener.css' %}" />

目录结构

/hello/static/hello/index.css

收集静态输出

Post-processed 'hello/screener.css' as 'hello/screener.4d9ca1ef5004.css'
Post-processed 'screener.css' as 'screener.4d9ca1ef5004.css'

121 static files copied to '/Users/user/Desktop/heroku-bayesian/python-getting-started/staticfiles', 151 post-processed.

推荐答案

将您的STATIC_URL更改为/static/,并且出于测试目的,只需设置您的STATICFILES_DIR = ['hello/static/hello']. Whitenoise不一定需要排在第二位.我在最下面运行我的游戏,它与Heroku上的Django多租户应用程序配合使用效果很好.我相信当您现在按下Heroku时,您可能不得不再次执行collectstatic.如果它在第一次构建后抱怨您的静态文件,只需将其禁用,就像Heroku所说的此处.

Change your STATIC_URL to /static/ and for testing purposes, just set your STATICFILES_DIR = ['hello/static/hello']. Whitenoise does not necessarily need to be 2nd. I run mine on the bottom and it works fantastic with my Django multi-tenant app on Heroku. I believe when you push to Heroku now, you will probably have to do a collectstatic again. If it complains about your static files when building after the first time, just disable it like Heroku says here.

Heroku不支持SQLite,因此您需要迁移到postgres.这是我做过的事,从不回头.您所要做的就是更改数据库信息和引擎,一切顺利.

By the way SQLite is not supported by Heroku and you will need to migrate over to postgres. This is something I did and never looked back. All you have to do is change the database information and engine and you are good to go.

这篇关于当源中有其他静态文件时,为什么不能在django项目中添加静态文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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