0

我需要帮助=)

来自静态文件夹的图像不会加载。

版本:Django 1.11.3

这是我的代码:

first_project/first_project/settings.py:

import os
 
 
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
 
TEMPLATE_DIR=os.path.join(BASE_DIR,"templates")
STATIC_DIR=os.path.join(BASE_DIR,"static")
 
...
 
INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'first_app',
]
 
...
 
# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/1.11/howto/static-files/
 
STATIC_URL = '/static/'
STATICFILES_DIRS=[
    STATIC_DIR,
    os.path.join(BASE_DIR,"first_app","static","first_app"),
]

first_project/templates/first_app/index.html:

<!DOCTYPE html>
{% load static %}
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Yp</title>
</head>
<body>
<h1>This is yp picture!</h1>
<!--It works -->
<img src="/static/first_app/yp.jpg" alt="oh oh">
<!--It doesn't work-->
<img scr="{% static "first_app/yp.jpg" %}" alt="oh oh">
 
</body>
</html>

first_project/static/first_app/yp.jpg - 图像的路径。

谢谢!

4

2 回答 2

1

你应该做几件事,但我认为你不这样做。

我只为你创建了一个简单的项目并添加到我的 GitHub

只需点击此链接:GitHub first 项目模板

如果有帮助,请投票。

于 2020-06-30T20:27:33.890 回答
1

首先,您写的是 scr,而不是 src,这应该更正。

    <!--It doesn't work-->
<img scr="{% static "first_app/yp.jpg" %}" alt="oh oh">

之后,您可能需要从代码中清除“first_app/”部分,因为您直接在设置文件中处理您的目录。

于 2020-06-30T22:28:41.733 回答