Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Don't server Static Files. #167

Closed
darwinv opened this issue Feb 12, 2018 · 7 comments
Closed

Don't server Static Files. #167

darwinv opened this issue Feb 12, 2018 · 7 comments

Comments

@darwinv
Copy link

darwinv commented Feb 12, 2018

I already commented to Andrew via Twitterr.I have the most recent versions of channels, daphne and asgi-ref, and the static files doesn't load.

On version 1.1.8 works Well. I already have the staticfiles turned on as an app

@andrewgodwin
Copy link
Member

Can I have:

  • The exact versions of python, channels, daphne, asgiref, django you are running
  • Your URLs file
  • Your settings file
  • The static URL that does not work
  • The path that static URL should serve (where the file is in your project)

@darwinv
Copy link
Author

darwinv commented Feb 12, 2018

daphne==2.0.3
channels==2.0.2
asgiref==2.1.4
Django==1.11.4
Python 3.5.2

my main urls.py

from django.conf.urls import include, url
from django.conf.urls.i18n import i18n_patterns

urlpatterns = i18n_patterns(
    # login
    url(r'^', include('login.urls')),
    # admin linkup
    url(r'^admin/', include('dashboard.urls')),
)
urlpatterns += [
    # api web
    url(r'^api/', include('api.urls')),
]

my urls of login app

from django.conf.urls import url

from . import views

app_name = 'login'
urlpatterns = [

    # login
    # ingreso por defecto
    url(r'^$', views.weblogin, name='indexlogin'),

    # requerido por Django para cuando rechazar un request de un
    # usuario que no esta autorizado
    url(r'^accounts/login/$', views.weblogin, name='login'),

    url(r'^logout/', views.logout_view, name='logout'),
]

## my urls of dashboard app

from django.conf.urls import url

from dashboard.views import actors, account, account_status, authorizations

app_name = 'dashboard'

urlpatterns = [

    url(r'^$', actors.Specialist().list, name='index'),

    # specialists
    url(r'^actor/specialists/$', actors.Specialist().list, name='actor-specialists-list'),
    url(r'^actor/specialists/(?P<pk>[0-9]+)$', actors.Specialist().detail, name='actor-specialists-detail'),
    url(r'^actor/specialists/edit/(?P<pk>[0-9]+)$', actors.Specialist().edit, name='actor-specialists-edit'),
    url(r'^actor/specialists/create/$', actors.Specialist().create, name='actor-specialists-create'),
    url(r'^actor/specialists/delete/$', actors.Specialist().delete, name='actor-specialists-delete'),

    # clients
    url(r'^actor/clients/$', actors.Client().list, name='actor-clients-list'),
    url(r'^actor/clients/(?P<pk>[0-9]+)$', actors.Client().detail, name='actor-clients-detail'),
    url(r'^actor/clients/edit/(?P<pk>[0-9]+)$', actors.Client().edit, name='actor-clients-edit'),
    url(r'^actor/clients/create/$', actors.Client().create, name='actor-clients-create'),
    url(r'^actor/clients/delete/$', actors.Client().delete, name='actor-clients-delete'),

    # sellers
    url(r'^actor/sellers/$', actors.Seller().list, name='actor-sellers-list'),
    url(r'^actor/sellers/(?P<pk>[0-9]+)$', actors.Seller().list, name='actor-sellers-detail'),
    url(r'^actor/sellers/edit/(?P<pk>[0-9]+)$', actors.Seller().list, name='actor-sellers-edit'),
    url(r'^actor/sellers/create/$', actors.Seller().create, name='actor-sellers-create'),

    # administrators
    url(r'^actor/administrators/$', actors.Administrator().list, name='actor-administrators-list'),
    url(r'^actor/administrators/(?P<pk>[0-9]+)$', actors.Administrator().list, name='actor-administrators-detail'),
    url(r'^actor/administrators/edit/(?P<pk>[0-9]+)$', actors.Administrator().list, name='actor-administrators-edit'),
    url(r'^actor/administrators/create/$', actors.Administrator().list, name='actor-administrators-create'),

    # Autorizaciones
    url(r'^authorizations/clients$', authorizations.AutorizationClient().list, name='authorizations-clients'),

    # Estados de cuenta
    url(r'^account_status/sellers/$', account_status.AccountStatusSeller().list, name='account-status-seller'),

]

My settings.py

from django.utils.translation import ugettext_lazy as _
import os
from django.db import models
from linkupapi.settings_secret import *
# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))

AUTH_USER_MODEL = 'api.User'
# Quick-start development settings - unsuitable for production
# See https://docs.djangoproject.com/en/1.11/howto/deployment/checklist/

# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = 'y9_571a#34)lk3cevqq*c(1_#d+h6bum!v7t7_8@ascpzc-@u@'

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

ALLOWED_HOSTS = ['*']


# Application definition

INSTALLED_APPS = [
    'api',
    'login',
    'dashboard',
    'client',
    'specialist',
    'seller',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'channels',
]


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

SESSION_ENGINE = "django.contrib.sessions.backends.file"


ROOT_URLCONF = 'linkup.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',
            ],
        },
    },
]


AUTHENTICATION_BACKENDS = ['login.APIBackend.APIBackend']
WSGI_APPLICATION = 'linkup.wsgi.application'
ASGI_APPLICATION = 'linkup.routing.application' #version 2.0


# Database
# https://docs.djangoproject.com/en/1.11/ref/settings/#databases
#
# Password validation
# https://docs.djangoproject.com/en/1.11/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.11/topics/i18n/

LANGUAGES = (
    ('es', _('Spanish')),
    ('en', _('English')),
)
# Set the default language for your site.
LANGUAGE_CODE = 'es'

# Tell Django where the project's translation files should be.
LOCALE_PATHS = (
    os.path.join(BASE_DIR, 'locale/static_db'),
    os.path.join(BASE_DIR, 'locale'),  # python manage.py makemessages -l en
)

TIME_ZONE = 'UTC'

USE_I18N = True

USE_L10N = True

USE_TZ = True

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

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

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

My routing.py

from channels.routing import ProtocolTypeRouter

application = ProtocolTypeRouter({
    # Empty for now (http->django views is added by default)
})

my asgi.py

import os
import django
from channels.routing import get_default_application # version 2.0

os.environ.setdefault("DJANGO_SETTINGS_MODULE", "linkup.settings")
django.setup() # version  2.0
application = get_default_application() # version 2.0

"The static URL that does not work:"
for example this css
http://ec2-54-152-42-68.compute-1.amazonaws.com:8080/static/bootstrap/css/bootstrap.min.css

"The path that static URL should serve (where the file is in your project)"
/home/ubuntu/dev/linkupbackedweb/staticfiles/

@andrewgodwin
Copy link
Member

Ahh, you're talking about running it in production via daphne, not runserver. Like Django, Daphne will only serve static files from the same process in local dev via runserver - once you deploy to production, you need to run collectstatic and serve static files separately as you can read here: https://docs.djangoproject.com/en/2.0/howto/static-files/deployment/#serving-static-files-in-production

@darwinv
Copy link
Author

darwinv commented Feb 13, 2018

Ohh I see. I recently noticed that when I was using Channels 1.8, I had this code on routing.py
on production

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

Its likely that was the reason. that on 1.8 was working and 2.0 don't

@andrewgodwin
Copy link
Member

Right. If you want to serve static files directly from the process still, you can use something like whitenoise which provides this as a Django app.

@ebsaral
Copy link

ebsaral commented Sep 20, 2018

@andrewgodwin While there is StaticFilesConsumer in 1.x, was it removed in 2.x of Channels? Is there a similar workaround? I don't know if that's the right place to ask this though.

@andrewgodwin
Copy link
Member

@ebsaral It's not the right place to ask it, but I'll reply anyway - there's similar code in Channels 2 in channels.staticfiles.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants