无法使用 Django 显示静态文件夹中的图像

https://img3.mukewang.com/64edc5d6000108a819151082.jpg

这是我的 home.html 我无法显示 static/images/ 文件夹中的图像。虽然 *[09/Mar/2020 15:52:09] "GET /static/images/mona.jpg HTTP/1.1" 404 1669 * 显示在终端中。

https://img.mukewang.com/64edc5e5000183e619171077.jpg


<head>

    <meta charset="UTF-8" />

    <meta name="viewport" content="width=device-width, initial-scale=1.0" />

    {% block content %}

    {% load static %}

    <title>Home</title>

    <link rel="stylesheet" href={% static '/css/style.css' %} />

    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css"

        integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous" />

    <script src="https://code.jquery.com/jquery-3.2.1.slim.min.js"

        integrity="sha384-KJ3o2DKtIkvYIK3UENzmM7KCkRr/rE9/Qpg6aAZGJwFDMVNA/GpGFF93hXpG5KkN"

        crossorigin="anonymous"></script>

    <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.12.9/umd/popper.min.js"

        integrity="sha384-ApNbgh9B+Y1QKtv3Rn7W3mgPxhU9K/ScQsAP7hUibX39j7fakFPskvXusvfa0b4Q"

        crossorigin="anonymous"></script>

    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/js/bootstrap.min.js"

        integrity="sha384-JZR6Spejh4U02d8jOt6vLEHfe/JQGiRRSQQxSfFWpi1MquVdAyjUar5+76PVCmYl"

        crossorigin="anonymous"></script>

</head>


<body>

    {% include "includes/header.html" %}


    <div class="pt-5">

        <div class="card d-inline-block">

            <img src={% static 'images/mona.jpeg' %} alt="Avatar" style="width:100%;">

            <div class="container">

                <h4><b>Mona Lisa</b></h4>

                <p><a href="#">Architect & Engineer</a></p>

            </div>

        </div>


        <div class="card d-inline-block">

            <img src={% static "images/mona.jpg" %} alt="Avatar" style="width:100%">

            <div class="container">

                <h4><b>The Scream</b></h4>

                <p><a href="#">Architect & Engineer</a></p>

            </div>

        </div>

    </div>


    {% include "includes/footer.html" %}

    {% endblock content %}

</body>


米脂
浏览 119回答 3
3回答

慕的地6264312

在源周围添加引号,如下所示{%&nbsp;load&nbsp;static&nbsp;%} <img&nbsp;src="{%&nbsp;static&nbsp;'images/mona.jpeg'&nbsp;%}"&nbsp;alt="Avatar"&nbsp;style="width:100%;">

侃侃尔雅

在生产环境中,静态文件由 Web 服务器(apache、nginx)提供在开发中,如果您使用 django.contrib.staticfiles 并且 DEBUG 设置为 True,则 app_name/static/app_name 中的文件将自动提供。如果文件是由用户在您的网络应用程序中上传的,您需要编辑您的基本 urls.py 文件,如下所示from django.conf.urls.static import staticurlpatterns = [&nbsp; &nbsp; # ... the rest of your URLconf goes here ...] + static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)For more info on this see&nbsp; https://docs.djangoproject.com/en/3.0/howto/static-files/

牛魔王的故事

抱歉各位,这是一个拼写错误。我真是太笨了....在设置.pySTATICFILES_DIRS = [&nbsp;os.path.join(BASE_DIR, 'the7thstreet/static'),&nbsp;'/var/www/static',&nbsp;]已更正STATICFILES_DIRS = [&nbsp;os.path.join(BASE_DIR, 'static'),&nbsp;'/var/www/static',&nbsp;]
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Html5