I have a Django application which I'm not able to load static files in. My templates are perfectly loaded, but CSS and JS files can't be loaded. Here's my files:
zeinab@ZiZi:~/Desktop/MyProject$ tree . ├── __init__.py ├── manage.py ├── myapp │ ├── __init__.py │ ├── lib │ │ ├── decorators.py │ │ ├── dorsa_forms.py │ │ ├── __init__.py │ │ └── utils.py │ ├── log │ ├── media │ │ └── admin │ │ └── background_login │ │ └── bg1.jpg │ ├── settings.py │ ├── static │ │ ├── calendar.js │ │ └── inbox.css │ ├── templates │ │ ├── 404.html │ │ ├── base.html │ │ └── index.html │ ├── urls.py │ ├── views.py │ └── wsgi.py └── requirements.txt 8 directories, 18 files When I runserver, I get "GET /static/file/url HTTP/1.1" 404 1265 for each CSS and JS file. Here's MyProject/manage.py:
#!/usr/bin/env python import os import sys if __name__ == "__main__": os.environ.setdefault("DJANGO_SETTINGS_MODULE", "myapp.settings") from django.core.management import execute_from_command_line execute_from_command_line(sys.argv) This is MyProject/myapp/settings.py:
import os from django.contrib.messages import constants as messages BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) INSTALLED_APPS = ( 'django.contrib.admin', 'django.contrib.auth', 'django.contrib.contenttypes', 'django.contrib.sessions', 'django.contrib.messages', 'django.contrib.staticfiles', 'sorl.thumbnail', 'myapp.lib', ) MIDDLEWARE_CLASSES = ( 'django.middleware.security.SecurityMiddleware', 'django.contrib.sessions.middleware.SessionMiddleware', 'django.middleware.common.CommonMiddleware', 'django.middleware.csrf.CsrfViewMiddleware', 'django.middleware.cache.UpdateCacheMiddleware', 'django.middleware.locale.LocaleMiddleware', 'django.middleware.cache.FetchFromCacheMiddleware', 'django.contrib.auth.middleware.AuthenticationMiddleware', 'django.contrib.auth.middleware.SessionAuthenticationMiddleware', 'django.contrib.messages.middleware.MessageMiddleware', 'django.middleware.clickjacking.XFrameOptionsMiddleware', ) SITE_ID = 1 AUTHENTICATION_BACKENDS = ( 'django.contrib.auth.backends.ModelBackend', ) ROOT_URLCONF = 'myapp.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.i18n', 'django.template.context_processors.request', 'django.contrib.auth.context_processors.auth', 'django.contrib.messages.context_processors.messages', 'django.template.context_processors.i18n', ], }, }, ] WSGI_APPLICATION = 'myapp.wsgi.application' STATIC_URL = '/static/' STATICFILES_DIRS = ( os.path.join(BASE_DIR, "static"), ) STATIC_ROOT = '/myapp/static/' MEDIA_ROOT = os.path.join(BASE_DIR, "media") MEDIA_URL = '/media/' LOG_PATH = os.path.join(BASE_DIR, 'log') DEBUG = True This is MyProject/myapp/urls.py:
from django.conf import settings from django.conf.urls import include, url from django.conf.urls.static import static from django.contrib import admin from django.contrib.staticfiles.urls import staticfiles_urlpatterns from .views import default urlpatterns = [ url(r'^$', default, name='default'), url(r'^admin/', admin.site.urls), ] urlpatterns += staticfiles_urlpatterns() urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT) This is MyProject/myapp/wsgi.py:
import os from django.core.wsgi import get_wsgi_application os.environ.setdefault("DJANGO_SETTINGS_MODULE", "myapp.settings") application = get_wsgi_application() EDIT 1:
I have changed MyProject/myapp/urls.py to the following:
from django.conf import settings from django.conf.urls import include, url from django.views import static from django.contrib import admin from .views import default urlpatterns = [ url(r'^$', default, name='default'), url(r'^admin/', admin.site.urls), url(r'^public/(?P<path>.*)$', static.serve, { 'document_root': settings.MEDIA_ROOT }, name='url_public'), ] I ended up to the same results.
EDIT 2:
I also tried using urlpatterns += static(settings.STATIC_URL, document_root=settings.STATIC_ROOT) instead of urlpatterns += staticfiles_urlpatterns() and then trying to collectstatic; collectstatic completed successfully, but still opening URLs returns "GET /static/file/url HTTP/1.1" 404 1265.