Accessing static in django

2019-07-24 16:05发布

I'm having trouble sorting my static directory and linking css files through templates in html pages with django. I keep getting the error "Not Found: /CSS/bootstrap.min.css"

I know this is a problem with how my directory is set up in settings.py but I can't seem to fix the issue. Below is my code for settings.py and layout.html (the page i'm using the call the css file).

layout.html

{% load staticfiles %}
<!DOCTYPE html>

<html lang="en" xmlns="http://www.w3.org/1999/xhtml">
<head>
    <meta charset="utf-8" />
    <title>Testing {% block title %}{% endblock %}</title>
    <link href="{% static 'css/bootstrap.min.css' %}"  type="text/css" rel="stylesheet">
</head>
<body>


    <div class="container">
    {% block content %}

    {% endblock %}

    </div>

</body>
</html>

settings.py

BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/1.10/howto/static-files/


STATIC_URL = 'C:/Users/Luke/Desktop/Capstone/CapstoneNotespool/capstonenotespool/capstonenotespool/static/'

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

file path

2条回答
Summer. ? 凉城
2楼-- · 2019-07-24 16:47

You are almost there! You just need to add the correct static file directory to your STATICFILES_DIRS.

BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
# STATIC_URL tells django the url path to use for all static files. It 
# doesn't really have anything to do with your file locations on your 
# computer.
STATIC_URL = '/static/'

STATICFILES_DIRS = [
    os.path.join(BASE_DIR, "static"),
    # Add this line here.
    os.path.join(BASE_DIR, "capstonenotespool", "static"),
]
查看更多
贼婆χ
3楼-- · 2019-07-24 16:51

I think You must check again your static url, I think you config be wrong.

Here is answer you looking for.

Example of tree file

And this is my config for this

STATIC_URL = '/static/'

if DEBUG:

MEDIA_URL = '/media/'

STATIC_ROOT = os.path.join(os.path.dirname(BASE_DIR), "static", >"static-only")

MEDIA_ROOT = os.path.join(os.path.dirname(BASE_DIR), "static", >"media")

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

)

查看更多
登录 后发表回答