I have tried to modify in the settings.py file to allow loading css files but some of html code still doesn't appear with its style, the run in the terminal the command "python manage.py collectstatic" and it says that files are copied but still doesn't appear its effect.
HERE IS THE MODIFIED LINES IN settings.py file
STATIC_ROOT = '/static/'
STATICFILES_DIRS = [
os.path.join(BASE_DIR, "static"),
]
STATIC_URL = '/static/'
In your settings.py provide:
STATICFILES_FINDERS = [
'django.contrib.staticfiles.finders.FileSystemFinder',
'django.contrib.staticfiles.finders.AppDirectoriesFinder',
]
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
STATICFILES_DIRS = (os.path.join(BASE_DIR, 'static_files'),
)
STATIC_ROOT = os.path.join(BASE_DIR, 'static', )
STATIC_URL = 'http://example.com/static/'
So if static files finders are defined and static_files is declared as source for static files, whatever you place in
/your_root/static_files/
will come into
/your_root/static
after you run
python manage.py collectstatic
The mistake with
STATICFILES_DIRS = [
os.path.join(BASE_DIR, "static"),
]
is that you're trying to take from and bring to the same directory, which is the destination directory of all static files collected.
It is better to set STATIC_ROOT = os.path.join(BASE_DIR, 'static_root')
If you have a folder called static
in BASE_DIR, add
STATICFILES_DIRS = (
os.path.join(BASE_DIR, 'static'),
)
You may add static_root/
to .gitignore
file after you run python manage.py collectstatic
The way I use is:
2.In Html
first load static files {% load static %}
then link to the path
href = " {% static 'name_of_app/fileName ' %} "
You can add these codes in your settings.py in latest django version
STATIC_URL = '/static/'
MEDIA_URL = '/media/'
STATICFILES_DIRS = [os.path.join(BASE_DIR, 'static_in_env')]
VENV_PATH = os.path.dirname(BASE_DIR)
STATIC_ROOT = os.path.join(VENV_PATH, 'static_root')
MEDIA_ROOT = os.path.join(VENV_PATH, 'media_root')
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.