静态文件不会被找到

时间:2017-05-31 08:42:33

标签: python django django-staticfiles

在我的base.html中,我正在加载静态文件。 我认为在某种程度上它找不到路径。

我只想使用静态文件:
  {% load static %}   {% static 'css/bootstrap.min.css' %}   {% static 'css/style.css' %}

这是我的地图结构

  • ToetsOmgeving
    • _static
      • CSS
        • bootstrap.min.css
        • 的style.css
      • JS​​
    • _templates
      • base.html文件
      • 测试
        • test_create.html
    • 测试
    • testenvironment
      • settings.py
    • 用户
    • db.sqlite3
    • manage.py

这是我的settings.py:

导入os

BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
STATIC_DIR = os.path.join(BASE_DIR, '_static')

SECRET_KEY = 'd5-f7&tlic%)*-ol)kc8$0psb+m^l_9k4!l$l(ie$#wd=#*1is'
DEBUG = True

ALLOWED_HOSTS = []

INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',

    'test',
    'user',
]

MIDDLEWARE = [
    'django.middleware.security.SecurityMiddleware',
    'django.contrib.sessions.middleware.SessionMiddleware',
    'django.middleware.common.CommonMiddleware',
    'django.middleware.csrf.CsrfViewMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'django.contrib.messages.middleware.MessageMiddleware',
    'django.middleware.clickjacking.XFrameOptionsMiddleware',
]

ROOT_URLCONF = 'testenvironment.urls'

TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [os.path.join(BASE_DIR, '_templates')]
        ,
        'APP_DIRS': True,
        'OPTIONS': {
            'context_processors': [
                'django.template.context_processors.debug',
                'django.template.context_processors.request',
                'django.contrib.auth.context_processors.auth',
                'django.contrib.messages.context_processors.messages',
            ],
        },
    },
]

WSGI_APPLICATION = 'testenvironment.wsgi.application'


# Database
# https://docs.djangoproject.com/en/1.10/ref/settings/#databases

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.sqlite3',
        'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
    }
}


# Password validation
# https://docs.djangoproject.com/en/1.10/ref/settings/#auth-password-validators

AUTH_PASSWORD_VALIDATORS = [
    {
        'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator',
    },
    {
        'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator',
    },
    {
        'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator',
    },
    {
        'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator',
    },
]

# Internationalization
# https://docs.djangoproject.com/en/1.10/topics/i18n/

LANGUAGE_CODE = 'en-us'

TIME_ZONE = 'UTC'

USE_I18N = True

USE_L10N = True

USE_TZ = True


# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/1.10/howto/static-files/

STATIC_URL = '/static/'
STATIC_ROOT = STATIC_DIR

STATICFILES_DIRS = (
  os.path.join(BASE_DIR, '/_static/'),
)

我的base.html:

{% load static %}
# I tried to do {% load staticfiles %} but also didn't work
<!DOCTYPE html>
<html lang="en">
<head>
{% block head %}
    <meta charset="UTF-8">
    <title>{% block title %}{% endblock title %} - ToetsOmgeving!</title>

    <link type="text/css" rel="stylesheet" href="{% static 'css/bootstrap.min.css' %}">
    <link type="text/css" rel="stylesheet" href="{% static 'css/style.css' %}">
    {% block css %}{% endblock css %}
{% endblock head %}
</head>
<body>
{% block content %}

{% endblock content %}

<script type="text/javascript" src="{% static 'js/jquery.js' %}"></script>
<script type="text/javascript" src="{% static 'js/bootstrap.min.js' %}"></script>
{% block js %}
{% endblock js %}
</body>
</html>

3 个答案:

答案 0 :(得分:0)

你必须将你的css文件夹移动到_static目录下,因为它是STATIC_DIR

答案 1 :(得分:0)

设置PROJECT_DIR = os.path.abspath(os.path.join(BASE_DIR, os.path.pardir)) STATICFILES_DIRS = ( os.path.join(PROJECT_DIR, '_static'),

答案 2 :(得分:0)

对你们所有人来说,我做过的一件蠢事就是,在test app

中添加一个静态文件夹

自从我添加它以后,它可以工作:)