0

I have been trying to create a django application, but there is a problem with django rendering static files:

Here is my setting files static configuration:

STATIC_URL = '/static/' STATIC_ROOT = os.path.join(BASE_DIR, "static") 

I have placed all the static files in a static folder inside the location where manage.py resides.

4
  • Is this in development or production Commented Jul 23, 2017 at 10:01
  • this is in development! Commented Jul 23, 2017 at 10:12
  • Do you have DEBUG set to True? Commented Jul 23, 2017 at 10:16
  • Also, check your browser's dev tools. It could just be an incorrect path. Commented Jul 23, 2017 at 10:18

2 Answers 2

1

Here you defined an STATIC_ROOT, so you after placing your static files and on each change of your css or js files you need to run

python manage.py collectstatic

This command will gather all static files into the defined dictionary, but this is done after the completion of development. So instead of that you can use STATICFILES_DIRS during development,

STATICFILES_DIRS = [ os.path.join(BASE_DIR, "static") ] 

which will automatically find the static files during your development period.

Also make sure you have staticfiles in installed apps.

INSTALLED_APPS = [ 'django.contrib.admin', 'django.contrib.auth', 'django.contrib.contenttypes', 'django.contrib.sessions', 'django.contrib.messages', 'django.contrib.staticfiles', # it's is used for static files ] 

In your template you can load static files like

{% load staticfiles %} # register static tag <link ref="stylesheet" href="{% static 'style.css' %}"> 
Sign up to request clarification or add additional context in comments.

Comments

0

settings.py

INSTALLED_APPS = [ ... ... 'django.contrib.staticfiles', ... ... ] STATIC_URL = '/static/' STATICFILES_DIRS = [ os.path.join(BASE_DIR, "static"), ] STATIC_ROOT = os.path.join(os.path.dirname(BASE_DIR), "static_cdn") 

urls.py (main project's urls.py)

from django.conf.urls.static import static urlpatterns = [ ... ] if settings.DEBUG: urlpatterns += static(settings.STATIC_URL, document_root=settings.STATIC_ROOT) 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.