我需要帮助=)
来自静态文件夹的图像不会加载。
版本: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 - 图像的路径。
谢谢!