猿问

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

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

http://img2.mukewang.com/6307267d0001cbbb19161079.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>


ABOUTYOU
浏览 104回答 3
3回答

波斯汪

在源周围添加引号,如下所示{%&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中的文件。如果文件由用户在 Web 应用中上传,则需要编辑基本 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/

呼如林

对不起,伙计们,这是一个拼写错误。太笨了...在 settings.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

相关分类

Python
我要回答