简体   繁体   中英

css and other static files not working in django

The current project I've started working on, uses django 2.2 and most of the links are hardcoded.

All static content is in folder named media and used in base.html as follows

in base.html

{% load staticfiles %} ---- using this as first line of base.html
.......
.......
<head>
<link href="/media/css/backoffice/font-awesome.min.css" rel="stylesheet" type="text/css">
</head>

in settings.py

STATIC_URL = '/media/'

STATICFILES_DIRS = (
    os.path.join(BASE_DIR, 'media'),
)

STATIC_ROOT = os.path.join(BASE_DIR, 'media/')

STATICFILES_FINDERS = (
    'django.contrib.staticfiles.finders.FileSystemFinder',
    'django.contrib.staticfiles.finders.AppDirectoriesFinder',
#    'django.contrib.staticfiles.finders.DefaultStorageFinder',
)

I've also added following to the main urls.py file:

from django.conf.urls.static import static
urlpatterns += static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)

Yet I'm not able to render the static file listed in the head section of base.html

I've also experimented with commenting out static root and staticfiles_dir but it does not work

You need to put all files in project root's static folder and run this command:

python manage.py collectstatic

For loading static files in the template you need to write this:

{% load static %}

And in all the static files like css , js and images you can do like this:

{% static "<absolute path of file>" %}

And you can refer from documentation .

I hope this will help you!

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