Django不会显示静态文件

时间:2018-11-20 10:34:22

标签: css django

<!DOCTYPE html>
{% load staticfiles %}
<html>
........
<img src="{% static 'images/parralax/spaces.jpg' %}"  width = 250px  />
<body>

以上代码不会显示我作为背景的图像。真的不确定如何解决此问题。

这是我的设置文件。我认为问题来自链接到CSS无效。 预先感谢!

BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
TEMPLATE_DIR = os.path.join(BASE_DIR, 'templates')
STATIC_DIR = os.path.join(BASE_DIR, 'static')
MEDIA_DIR = os.path.join(BASE_DIR,'media')


TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [TEMPLATE_DIR,],
        'APP_DIRS': True,
        'OPTIONS': {
            'context_processors': [.....
            ],
        },
    },
]

STATICFILES_DIRS = [STATIC_DIR,  ]
STATIC_URL = '/static/'
MEDIA_ROOT = MEDIA_DIR #where to look for files
MEDIA_URL = '/media/' #where to serve files from on url
WSGI_APPLICATION = 'Space.wsgi.application'
STATIC_ROOT = os.path.join(BASE_DIR, 'static_root')

.....


.....
urls.py
urlpatterns = [
    url(r'^$', views.index, name='index'),
    url(r'^Spaces/', include('Spaces.urls')),
    #Django Admin
    path('admin/', admin.site.urls),
    #User Management
    url(r'^accounts/', include('allauth.urls')),
    path(r'^users/', include('users.urls')),
    path(r'^users/', include('django.contrib.auth.urls')),
]

urlpatterns += static(settings.STATIC_URL, document_root=settings.STATIC_DIR)
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_DIR)

请帮助:)

干杯!

4 个答案:

答案 0 :(得分:2)

确保将以下行写在html文件的顶部:

{% load static %}

第二点是关闭您的div:

</div>

然后将一些内容写入div或为您的潜水提供一个“高度”属性:

<div class="full-screen force-full-screen" style="background: url('{% static 'images/parallax/home/9.jpg' %}') center center no-repeat; background-size: cover;height: 100px;">

第三点:请确保这两行在您的settings.py中:

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

希望这会有所帮助。

答案 1 :(得分:1)

使用

{%从静态文件%中加载静态内容}

答案 2 :(得分:1)

在HTML文件的开头({% load static %}除外)使用base.html代替{% load staticfiles %}

并确保在您的settings.py

中可用

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

答案 3 :(得分:0)

我已解决此问题。

我一直都在链接错误的文件夹。愚蠢的错误。

感谢大家的帮助:)

相关问题