2017-02-12 70 views
1

我想我的手在这个例子中的WebSockets Django的,https://github.com/jacobian/channels-example因为我有打算用它我生产中的应用,以及其在Heroku的托管,并使用白噪声存在。 因此,克隆上述例子后,我做了一些调整使用白噪声,但现在通过任何浏览器到应用程序第一次访问(铬或FF)的静态文件不加载,当我再次刷新加载静态文件,并在第三次加载它再次熄灭等。 这里是我的设置文件:静态文件使用Django渠道服务问题

import os 
import random 
import string 
import dj_database_url 

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

SECRET_KEY = os.environ.get("SECRET_KEY", "".join(random.choice(string.printable) for i in range(40))) 
DEBUG = os.environ.get("DEBUG", False) 

# Application definition 
INSTALLED_APPS = (
    'django.contrib.admin', 
    'django.contrib.auth', 
    'django.contrib.contenttypes', 
    'django.contrib.sessions', 
    'django.contrib.messages', 
    'django.contrib.staticfiles', 
    'channels', 
    'chat', 
) 

MIDDLEWARE_CLASSES = (

    'django.middleware.security.SecurityMiddleware', 
    'whitenoise.middleware.WhiteNoiseMiddleware', 
    'django.contrib.sessions.middleware.SessionMiddleware', 
    'django.middleware.common.CommonMiddleware', 
    'django.middleware.csrf.CsrfViewMiddleware', 
    'django.contrib.auth.middleware.AuthenticationMiddleware', 
    'django.contrib.auth.middleware.SessionAuthenticationMiddleware', 
    'django.contrib.messages.middleware.MessageMiddleware', 
    'django.middleware.clickjacking.XFrameOptionsMiddleware', 

) 

ROOT_URLCONF = 'chat.urls' 

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.contrib.messages.context_processors.messages', 
      ], 
      'debug': DEBUG, 
     }, 
    }, 
) 

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

DATABASES = { 
    'default': dj_database_url.config(default="postgres:///channels-example", conn_max_age=500) 
} 

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.9/topics/i18n/ 
LANGUAGE_CODE = 'en-us' 
TIME_ZONE = 'UTC' 
USE_I18N = True 
USE_L10N = True 
USE_TZ = True 

# Honor the 'X-Forwarded-Proto' header for request.is_secure() 
SECURE_PROXY_SSL_HEADER = ('HTTP_X_FORWARDED_PROTO', 'https') 

# Allow all host headers 
ALLOWED_HOSTS = ['*'] 

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

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

# Extra places for collectstatic to find static files. 
STATICFILES_DIRS = [ 
    os.path.join(BASE_DIR, 'static'), 
] 

# Channel settings 
CHANNEL_LAYERS = { 
    "default": { 
     "BACKEND": "asgi_redis.RedisChannelLayer", 
     "CONFIG": { 
      "hosts": [os.environ.get('REDIS_URL', 'redis://localhost:6379')], 
     }, 
     "ROUTING": "chat.routing.channel_routing", 
    }, 
} 

# Logging 
LOGGING = { 
    'version': 1, 
    'disable_existing_loggers': False, 
    'handlers': { 
     'console': { 
      'class': 'logging.StreamHandler', 
     }, 
    }, 
    'loggers': { 
     'django': { 
      'handlers': ['console'], 
      'propagate': True, 
      'level': 'INFO' 
     }, 
     'chat': { 
      'handlers': ['console'], 
      'propagate': False, 
      'level': 'DEBUG', 
     }, 
    }, 
} 

STATICFILES_STORAGE = 'whitenoise.django.GzipManifestStaticFilesStorage' 

这里是我做了修改routing.py其他文件:

from channels.staticfiles import StaticFilesConsumer 
from . import consumers 

channel_routing = { 
    # This makes Django serve static files from settings.STATIC_URL, similar 
    # to django.views.static.serve. This isn't ideal (not exactly production 
    # quality) but it works for a minimal example. 
    # 'http.request': StaticFilesConsumer(), 

    # Wire up websocket channels to our consumers: 
    'websocket.connect': consumers.ws_connect, 
    'websocket.receive': consumers.ws_receive, 
    'websocket.disconnect': consumers.ws_disconnect, 
} 

Procfile是:

web: daphne chat.asgi:channel_layer --port $PORT --bind 0.0.0.0 -v2 
worker: python manage.py runworker -v2 

我还没有尝试过在Heroku上好像现在,只需在本地主机上我观察到了这种行为,从而didnt甚至集成与原始应用程序的解决方案。我用heroku本地在本地运行应用程序。

我在做什么错?而在Procfile生产中提及的过程准备在Heroku?

感谢

回答

0

尝试升级你的Django渠道1.0.3和3.3.0白噪声你提到的是使用过时的版本,其中添加到允许达芙妮服务与白噪声静态内容的一些bug修复的例子。或者你可以利用在S3存储服务的静态内容这是为Django的静态内容的首选方法。

https://github.com/django/channels/issues/87