Home > database >  Django cant find static files 404error
Django cant find static files 404error

Time:07-19

I'm using django to create apps in the project. I'm going to make common files into templates and static folders under the project folder of the project. However, when the server is running, the files in the templates are found well, but the files in the static folder are not imported. I tried a few attempts through a search, but it didn't work, so I ask questions. I would appreciate an answer.

setting.py

STATIC_URL = '/static/'
STATIC_DIRS = [
    os.path.join(BASE_DIR, 'static'),
    # BASE_DIR / 'static',
]
# STATIC_ROOT = os.path.join(BASE_DIR, 'assets')

my folder structure

project
-app1
-app2
-templates
  -app1
    -index.html
  -app2
    -index.html
-static
  -style.css
  -app.js

html code

{% load static %}
<link href="{% static 'style.css' %}" rel="stylesheet" />

CodePudding user response:

There is a typo in settings.py.

Instead of:

STATIC_DIRS = [
    os.path.join(BASE_DIR, 'static'),
    # BASE_DIR / 'static',
]

Write this:

STATICFILES_DIRS = [
    os.path.join(BASE_DIR, 'static'),
    # BASE_DIR / 'static',
]

CodePudding user response:

Have you got django.contrib.staticfiles included in your INSTALLED_APPS ?

https://docs.djangoproject.com/en/4.0/howto/static-files/

  • Related