Django图像未加载页面未找到错误

时间:2020-10-18 16:20:23

标签: html css django django-templates

我是Django的新手,并且面临加载静态文件(图像)的问题。我有

127.0.0.1/:1707 GET http://127.0.0.1:8000/static/css/static/images/background/1.jpg 404 (Not Found)

我非常确定问题出在路径上,但我不知道此/ static / css /的来源。你能帮我吗?

我的settings.py文件:

STATIC_ROOT = os.path.join(BASE_DIR, 'static')
STATIC_URL = '/static/'
STATICFILES_DIRS = [
    os.path.join(BASE_DIR, 'evillio/static')
]

我的index.html文件:

<!-- First Page with Picture and Search Starts -->
    <section class="home-one home1-overlay **home1_bgi1**">
        <div class="container">
            <div class="row posr">
                <div class="col-lg-12">
                    <div class="home_content">
                        <div class="home-text text-center">
                            <h2 class="fz55">Find Your Dream Home</h2>
                            <p class="fz18 color-white">From as low as $10 per day with limited time offer discounts.</p>
                        </div>

我的CSS文件:

home1_bgi1{
  background-image: url('static/images/background/1.jpg');
  -webkit-background-size: cover;
  background-size: cover;
  background-position: center center;
  height: 960px;

检查页面时

Inspect

1 个答案:

答案 0 :(得分:0)

您在顶部包含{%load static%}吗?

对于最新版本的Django,它在Setting.py中不使用它:

`STATIC_ROOT = os.path.join(BASE_DIR, 'static')
STATIC_URL = '/static/'
STATICFILES_DIRS = [
    os.path.join(BASE_DIR, 'evillio/static')
]`

对于Django 3.1,请改为以下代码:

`STATICFILES_DIRS = [
    BASE_DIR / "static",
    '/var/www/static/',
]`

阅读Django 3.1的文档,它在Django的早期版本中更新了一些代码。 https://docs.djangoproject.com/en/3.1/howto/static-files/