Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/87.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 无法在Django中加载静态文件_Python_Html_Django - Fatal编程技术网

Python 无法在Django中加载静态文件

Python 无法在Django中加载静态文件,python,html,django,Python,Html,Django,我试图添加一个图像到一个非常基本的html模板。我能够呈现html文件,但不幸的是图像将不会显示 这是我的错误: Not Found: /exapp/pic.jpg [21/Apr/2018 18:43:49] "GET /exapp/pic.jpg HTTP/1.1" 404 2145 我注意到django正在查找路径为/exapp/pic.jpg的文件,因此我在静态目录中创建了一个名为exapp的文件夹,并将图像放在其中。仍然没有图像:( 文件结构如下: ─ db.sqlite3

我试图添加一个图像到一个非常基本的html模板。我能够呈现html文件,但不幸的是图像将不会显示

这是我的错误:

Not Found: /exapp/pic.jpg
[21/Apr/2018 18:43:49] "GET /exapp/pic.jpg HTTP/1.1" 404 2145
我注意到django正在查找路径为/exapp/pic.jpg的文件,因此我在静态目录中创建了一个名为exapp的文件夹,并将图像放在其中。仍然没有图像:(

文件结构如下:

    ─ db.sqlite3
├── exapp
│   ├── admin.py
│   ├── apps.py
│   ├── __init__.py
│   ├── migrations
│   │   ├── __init__.py
│   │   └── __pycache__
│   │       └── __init__.cpython-35.pyc
│   ├── models.py
│   ├── __pycache__
│   │   ├── admin.cpython-35.pyc
│   │   ├── __init__.cpython-35.pyc
│   │   ├── models.cpython-35.pyc
│   │   ├── urls.cpython-35.pyc
│   │   └── views.cpython-35.pyc
│   ├── static
│   │   └── exapp
│   │       └── pic.jpg
│   ├── templates
│   │   └── index.html
│   ├── tests.py
│   ├── urls.py
│   └── views.py
├── manage.py
└── mysite
    ├── __init__.py
    ├── __pycache__
    │   ├── __init__.cpython-35.pyc
    │   ├── settings.cpython-35.pyc
    │   ├── urls.cpython-35.pyc
    │   └── wsgi.cpython-35.pyc
    ├── settings.py
    ├── urls.py
    └── wsgi.py
下面是有问题的HTML文件:

    <html>
    <head> 
        <title> Hello world</title>
    </head>

    <body>
        Look it worked 
    {% load static %}
    <img src="pic.jpg" alt="Smiley face" height="200" width="420">
    </body>

</html>
以下是我的视图和URL文件:

 ---------url.py in mysite folder------------
    from django.contrib import admin
    from django.urls import path, include

    urlpatterns = [
        path('admin/', admin.site.urls),
        path('exapp/',include('exapp.urls')),
    ]
    -----------urls.py in exapp folder--------------
    from django.urls import path


    from . import views 

    urlpatterns = [
        path('', views.index, name='index'),
    ]

 ----------Views in exapp folder--------------------------
    from django.shortcuts import render

    # Create your views here.
    from django.http import HttpResponse


    def index(request):
        return render(request,'index.html')

因此,作为一个总结,HTML模板确实会呈现到页面上。但由于某种原因,我无法确定如何将HTML代码中链接的图像显示到屏幕上。所有内容似乎都在正确的位置。感谢您的帮助!

您应该尝试更改图像源

这就是你所拥有的:

<img src="pic.jpg" alt="Smiley face" height="200" width="420">

试试这个:

<img src="{% static "exapp/pic.jpg" %}" alt="Smiley face" height="200" width="420">

这对我有用

祝你好运

<img src="{% static "exapp/pic.jpg" %}" alt="Smiley face" height="200" width="420">