S
S
Sergey Eremin2021-04-19 20:10:18
Django
Sergey Eremin, 2021-04-19 20:10:18

Has anyone experienced that statics are not displayed in Django 3.2 (but sometimes they are)?

Came across something wonderful in Django 3.2... No static from some directories inside staticin dev mode... Nearly all except /static/admin/. I don't understand anything. In settings.pyeverything as usual, the same with other projects in which everything works:

from my_project.my_secret import MY_MEDIA_ROOT_DEV1, MY_STATIC_ROOT_DEV1

MEDIA_ROOT = MY_MEDIA_ROOT_DEV1
STATIC_ROOT = MY_STATIC_ROOT_DEV1
STATIC_BASE_PATH = STATIC_ROOT

# STATICFILES_DIRS = [
#     # MY_STATIC_ROOT_DEV1,
#     # Put strings here, like "/home/html/static" or "C:/www/django/static".
#     # Always use forward slashes, even on Windows.
#     # Don't forget to use absolute paths, not relative paths.
#     # BASE_DIR,
#     MY_STATIC_ROOT_DEV1 + '/js',
#     MY_STATIC_ROOT_DEV1 + '/img',
#     MY_STATIC_ROOT_DEV1 + '/webfonts',
#     MY_STATIC_ROOT_DEV1 + '/css',
#     MY_STATIC_ROOT_DEV1 + '/svgs',
# ]

In url.pythe same way as in other projects:

from django.contrib import admin
from django.urls import path
from django.conf.urls.static import static
from django.conf.urls import url
from my_project import settings
from web import views

urlpatterns = [
    path('a-d-m-i-n/', admin.site.urls),

    url(r'^$', views.index),
]

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

When you do python manage.py collectstaticall the static, as expected, it is collected in the static / admin directory and any of these files can be viewed. Like : 127.0.0.1:8000/static/admin/css/base.css ... but no files from other files are shown ! .py or not... And whether or not there is a line urlpatterns += static(settings.STATIC_URL, document_root=settings.STATIC_ROOT) in url.py ... And, what is especially strange, if any file is put in the static/ folder admin or there static/admin/js -- this file is also NOT DISPLAYED. Sees only the files that he draggedpython manage.py collectstatic... What kind of magic is this?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
maksam07, 2021-04-19
@Sergei_Erjemin

Honestly, I didn’t quite understand the code, but:
settings.py

STATIC_URL = "/staticfiles/"
STATIC_ROOT = os.path.join(BASE_DIR, "staticfiles")
STATICFILES_DIRS = [os.path.join(BASE_DIR, "static_files")]

urls.py
if bool(settings.DEBUG):
    urlpatterns += static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
    urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)

works great. I don’t do static in the static_files folder, I don’t do it in the collectstatic debug, because there is no need, I do only on sale.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question