3

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.

2 Answers 2

2

Replace

urlpatterns += staticfiles_urlpatterns() 

by

urlpatterns += static(settings.STATIC_URL, document_root=settings.STATIC_ROOT) 

staticfiles_urlpatterns() is used only for debug to have a display view for your static file. As mentioned on https://docs.djangoproject.com/en/1.11/ref/contrib/staticfiles/ it should never be used in production.

Note: STATIC_ROOT & MEDIA_ROOT are used only when you're collecting the static (collectstatic with manage.py). It specifies the absolute path where to store the static and media files (usually /var/www/myapp/static and /var/www/myapp/media)

Sign up to request clarification or add additional context in comments.

Comments

1

In your urls.py you forgot to say how to "compute" the static route files.

Instead of this:

urlpatterns += staticfiles_urlpatterns() urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT) 

Try this:

from django.views import static urlpatterns = [ # [...] url(r'^public/(?P<path>.*)$', static.serve, { 'document_root': settings.MEDIA_ROOT }, name='url_public'), ] 

and in your template files (you didn't give a sample), try:

{% load static %} <h1>{% static 'calendar.js' %}</h1> 

Dont forget to use PyCharm because it helps so much for such things (it recognizes the path and you'll get the autocompletion in the template files (Ctrl+Space)).

5 Comments

When I runserver, I get AttributeError: 'function' object has no attribute 'serve'. Do you use a specific version of Django? I'm in the latest which is 1.11.5.
Use PyCharm, go to the word "static", and pres "Alt-enter" "enter" so PyCharm will add the correct "import" on the top of your urls file, or manually add it: from django.views import static
That's the point: I use static from django.conf.urls.static as you can see in the question, but you're referring to static from django.views. Please mention that in your answer.
@ZeinabAbbasi Ok I've edited my answer, thanks for your suggestion. Is it working and did I answer / put you on the right track?
I still get that 404 which I have provided in the question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.