【发布时间】:2016-11-23 19:00:47
【问题描述】:
我的项目中的“静态文件”存在一些问题 我想简单地加载图像。 这是我的代码:
views.py
from django.shortcuts import render
from django.http import HttpResponse
from django.template import loader
# Create your views here.
def D3(request):
template = loader.get_template('appli1/D3.html')
context = {}
return HttpResponse(template.render(context, request))
urls.py
from django.conf.urls import url
from django.contrib.staticfiles.urls import staticfiles_urlpatterns
from . import views
urlpatterns = [
url(r'^D3$', views.D3, name='D3'),
]
D3.html
<!DOCTYPE html>
<html>
<head>
</head>
<body>
{% load staticfiles %}
<img src="{% static "appli1/testimg.png" %}" alt="My image"/>
</body>
</html>
settings.py
STATIC_URL = '/static/'
图片testimg.png在appli1/static/appli1/中
并且文件 D3.html 在 appli1/templates/appli1/ 中
感谢您的帮助!
编辑: 我的项目结构对我来说似乎很好,也许我错了。这是它的样子:
test_django/
manage.py
db.sqlite3
test_django/
__init__.py
settings.py
urls.py
wsgi.py
__pycache__/
...
appli1/
__init__.py
admin.py
apps.py
models.py
tests.py
urls.py
views.py
__pycache__/
...
migrations/
...
static/
appli1/
testimg.png
templates/
appli1/
D3.html
【问题讨论】:
-
尝试按照我在这里提供的答案:stackoverflow.com/questions/29957604/…
-
不,它不起作用...谢谢帮助!
-
你把
{% load staticfiles %}放在你的html上?改用这个:<img src="{% static 'appli1/testimg.png' %}" alt="My image"/>。图片文件拼写是否正确(区分大小写)? -
另外,请检查
<img>标记中的引号。 -
我把 {% load staticfiles %} 放在 html 文件的顶部(在 !DOCTYPE 上面),我检查了一下,图像拼写正确。