简体   繁体   中英

Django: Static files are not found

I'm trying to add some static files to my project. I have tried adding a static folder to my app.

I have two apps:

  1. Authentication (authn),
  2. Main (main).

I have a lot of CSS/js content and I don't really know which is the best method to save the files, maybe all static files should be in one directory 'staticfiles' or each app should have 'static' folder. I have tried adding static folder to one of my apps, at this time - authn . And by trying to load the static files I firstly did python manage.py collectstatic , that put my all files into one folder, but I got two different folders now - admin, main. After tried putting all my static files into a static folder in authn app, but the static files were not loading after that. Here are my project structure and some photos and logs of the console, pycharm. 在此处输入图像描述

在此处输入图像描述 在此处输入图像描述

You need to add authn's static to STATICFILES_DIRS. Detail

STATICFILES_DIRS=[
  os.path.join(PROJECT_PATH, 'static'),
  os.path.join(PROJECT_PATH, 'authn', 'static')
]
  1. make static folder outside the app
  2. inside static create two folder main and auth
  3. inside main and auth create css js img and another catagories of folder
  4. Put each app's static file into their folder.That is put main app's css into static/main/css/main.css like that

  5. In settings.py make some changes-

     BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) STATIC_URL = '/static/'

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM