K
K
KirillTrueno2022-01-13 18:28:09
Django
KirillTrueno, 2022-01-13 18:28:09

How to properly set up static in django?

Site structure:

public_html
django
-manage.py
-frontend
--static
---frontend
----favicon.svg


settings.py file:
STATIC_URL = '/static/'
STATIC_ROOT = "../public_html/"


Page template:
<link rel="icon" href="{% static 'frontend/favicon.svg' %}" type="image/svg+xml">


After the "python manage.py collectstatic" command, the favicon.svg file is copied to:

public_html
-frontend
--favicon.svg


But in the page template, the path to it is "/static/frontend/favicon.svg", but it should be just "/frontend/favicon.svg".

How to fix it?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexander Nesterov, 2022-01-13
@AlexNest

Well, you yourself specified the URL. On the other hand, while you are using the dev server ( ), you need to write in the root url.
STATIC_URL = '/static/'

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

PS
After the "python manage.py collectstatic" command, the favicon.svg file is copied to:

collectstatic needs to be run when the project is ready to copy the static to the folder from which nginx will distribute it (well, or another server, although this is rare)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question