【问题标题】:Django image not loading Page not Found ErrorDjango图像未加载页面未找到错误
【发布时间】:2021-02-01 12:51:01
【问题描述】:

我是 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;

检查页面时

【问题讨论】:

    标签: html css django django-templates


    【解决方案1】:

    您是否在顶部添加了 {% 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/

    【讨论】:

      猜你喜欢
      • 2016-04-24
      • 2017-01-15
      • 1970-01-01
      • 2017-07-17
      • 2013-07-30
      • 2020-05-17
      • 2017-11-18
      • 2017-04-10
      • 2019-11-20
      相关资源
      最近更新 更多