在Django中找不到(404)背景图片

时间:2020-01-26 13:52:23

标签: html css django

我打算在HTML页面的正文中设置背景图片,因此我很乐意尝试以下步骤:

  1. settings.py中设置STATIC_URL和STATIC_ROOT,并在urls.py中传递URL模式。
  2. 运行静态部署python manage.py collectstatic
  3. 将正文标签<body style="background-image: url('{% static '34.jpg' %}');">中的内联css传递到基本html文件中。

但这一切都导致我想到这个!

获取http://localhost:7000/static/34.jpg 404(未找到)

与往常一样,以下是该案的嫌疑人/内gui文件/代码:

1。 base.html

<!DOCTYPE html>
<html>
{% load static %}
    <head>
        <meta charset="utf-8">
        <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
        <title>HR Suite</title>
        <link rel="stylesheet" type="text/css" href="https://stackpath.bootstrapcdn.com/bootstrap/4.2.1/css/bootstrap.min.css">
        <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
        <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.6/umd/popper.min.js"></script>
        <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.2.1/js/bootstrap.min.js"></script>
        <style>
            header {
             background-color: #4dacff;
             padding: 30px;
             text-align: center;
             font-size: 60px;
             color: white;
            }
            body {
                font-family: Arial, Helvetica, sans-serif;
            }

        </style>
    </head>

    <body style="background-image: url('{% static '34.jpg' %}');">
        <header>
            <p>HR Suite</p>
        </header>
        <div class='row'>
            {% block content %}
            {% endblock %}
        </div>
    </body>
</html> 

2。 settings.py

BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))

# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = True

# Application definition

INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'crispy_forms',
    'uploads.core'
]

ROOT_URLCONF = 'uploads.urls'

TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [os.path.join(BASE_DIR, 'uploads/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',
                'django.template.context_processors.media',
            ],
        },
    },
]

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

MEDIA_URL = '/media/'
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
AKASH_ROOT = 'C:\\Users\\Akash\\Documents\\Test'

3。 urls.py

urlpatterns = [
    url(r'^admin/', admin.site.urls),

    url(r'^$', views.login_form, name='login_form'),

    url(r'^upload/$', views.simple_upload, name='simple_upload'),

    url(r'^drop_down/$', views.drop_down, name='drop_down'),
]

urlpatterns += static(settings.MEDIA_URL, document_root=settings.AKASH_ROOT)
urlpatterns += static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)

尽管如此,我一直在努力解决这种情况,并找出背后的原因(阅读元凶)。但是,如果您能看到一个,请分享您的想法。场景基于Django version 1.9.8

1 个答案:

答案 0 :(得分:0)

我在练习时也遇到了这个错误,这是我所做的,并且对我有用。 更改<body style="background-image: url('{% static '34.jpg' %}');">

<body style="background-image: url("static/34.jpg");">

希望这可以解决您的问题