【发布时间】:2011-11-10 10:50:59
【问题描述】:
对不起,我的问题很基本,我想在模板页面中显示图像,我正在工作时间,图像没有显示
我创建了一个新项目 SecondPrjt ,只有一个名为“index”的视图函数和一个名为 test.html 的模板,并在旁边的 SecondPrjt 文件夹中创建一个名为 static 的文件夹
并在其中创建文件夹图像并将所有需要的图像放在那里
urls.py:
urlpatterns = patterns('',
# Examples:
# url(r'^$', 'SecondPrjt.views.home', name='home'),
(r'^$','views.index'),
.........
)
urlpatterns += staticfiles_urlpatterns()
views.py
from django.shortcuts import render_to_response
def index(request):
return render_to_response('test.html')
test.html:
img src="{{ STATIC_URL }}images/img03.jpg" width="186" height="186"
settings.py
STATIC_ROOT = "C:/wamp/www/SecondPrjt/static"
STATIC_URL = '/static/'
STATICFILES_DIRS = (
"C:/wamp/www/SecondPrjt/static",
)
STATICFILES_FINDERS = (
'django.contrib.staticfiles.finders.FileSystemFinder',
'django.contrib.staticfiles.finders.AppDirectoriesFinder',
)
INSTALLED_APPS = (
'django.contrib.staticfiles',
)
谁能建议我有什么问题。注意我正在使用 windows 和django1.3 并使用开发服务器。提前致谢
我在浏览中请求http://127.0.0.1:8000/ 后得到以下信息
c:\wamp\www\SecondPrjt>python manage.py runserver
Validating models...
0 errors found
Django version 1.3, using settings 'SecondPrjt.settings'
Development server is running at http://127.0.0.1:8000/
Quit the server with CTRL-BREAK.
[05/Sep/2011 15:18:24] "GET / HTTP/1.1" 200 89
[05/Sep/2011 15:18:24] <b>"GET /images/img03.jpg HTTP/1.1" 404 2028
【问题讨论】:
-
我尝试像 一样对 test.html 进行硬编码,这也失败了
-
硬编码时,没有404信息,但静止图像不显示
-
我的 html 语法有什么问题吗?,在 Internet Explorer 中是有效的(硬编码时)
-
它在 IE 中工作但在硬编码时无法在其他地方工作的原因是因为你需要
file://语法......但是硬编码它真的告诉你除了文件存在的事实之外在您的计算机上。 -
我试过 file:///C:/wamp/www/SecondPrjt/static/images/img03.jpg ,当我运行127.0.0.1:8000 时也失败了。但是,当使用 mozilla firefox 右键单击 test.html 时,它会显示带有图像的页面。
标签: image python-2.7 django-1.3