Bug? django fails to serve static files in Windows 10

16 views
Skip to first unread message

red sky

unread,
Oct 2, 2019, 12:53:50 PM10/2/19
to Django users
my system: Windows 10 1903, Python 3.7.4, 64 bit

My directory structure

%USERPROFILE%/Desktop/myproject/manage.py
%USERPROFILE%/Desktop/myproject/some_app/(...)
%USERPROFILE%/Desktop/myproject/myproject/sqlite3.db
%USERPROFILE%/Desktop/myproject/myproject/static (...)
%USERPROFILE%/Desktop/myproject/myproject/media (...)
%USERPROFILE%/Desktop/myproject/myproject/settings (with files __init__.py, settings_base.py, settings_devel.py, settings_production.py)
%USERPROFILE%/Desktop/myproject/myproject/static (...)
%USERPROFILE%/Desktop/myproject/myproject/static/css/styles.css
%USERPROFILE%/Desktop/myproject/myproject/more_apps/(...)

My urls.py (relevant parts)

from .settings import ENVIRONMENT, MEDIA_URL, MEDIA_ROOT, STATIC_URL, STATIC_ROOT
(...)
if ENVIRONMENT=="development":
import debug_toolbar
urlpatterns += [path('__debug__/',include (debug_toolbar.urls))]
urlpatterns += static(MEDIA_URL,document_root=MEDIA_ROOT)
urlpatterns += static(STATIC_URL,document_root=STATIC_ROOT)
urlpatterns += router.urls


My settings_base.py (relevant part)

INSTALLED_APPS = [...
'django.contrib.staticfiles',
...]

MEDIA_ROOT = BASE_DIR + '/media/'
STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR , 'static')
STATIFCILES_DIRS = ["","//",STATIC_ROOT,"C://Users//alfre//Desktop//memes//memes//"]
MEDIA_URL='/media/'

My settings_base.py (relevant part)

MEDIA_ROOT = BASE_DIR + '/media/'
STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR , 'static')
#STATIFCILES_DIRS = ["","//",STATIC_ROOT,"C://Users//alfre//Desktop//memes//memes//"]
MEDIA_URL='/media/'

(The error happens with several variants of the STATICFILES_DIRS, even with that line disabled or with several combinations)

The template shows the adress right, so I guess that is not the problem. Furthermore, the issue also happens when I request the file manually (http://127.0.0.1:8000/static/css/styles.css).

The error is "GET /static/css/styles.css HTTP/1.1" 404 1767
 
Strangely enough, I have not had any error with MEDIA images (served by sorl_thumbnails).

Strange behaviour:

If I replace STATIC_URL = '/static/' with 'static/' my static files are SOMETIMES served (but only when requested from root) (it seems I have to start the server, request the page, change form /static/ to static/ and then request it again to have the css served!), but django toolbar static files are not (they give back a 404 error , but they complain about the path GET /myproject/static/debug_toolbar/styles.css HTTP/1.1" 404 1767

As you can expect, this issue is relevant for development, given the importance of stylesheets.

Finally, if I insert print(STATIC_ROOT) in my settings files, I get the correct directory displayed on the console. But the files are still not delivered.

Thank you.
Reply all
Reply to author
Forward
0 new messages