-1

我通过 django 教程很好,并且对它的进展感觉很好。

但后来我开始尝试使用这些建议来集成 matplotlib。第一步是包含一个静态图像,这就是我想要做的。细节:

我的文件层次结构如下所示:

djOakData:
    djOakData:
        __init__.py
        settings.py
        urls.py
        wsgi.py
    manage.py
    showCrime:
        __init__.py
        admin.py
        models.py
        templates:
            showCrime:
                index.html
        tests.py
        tstImage.JPG
        urls.py
        views.py
    sqlite.db
    templates:
        base_site.html

djOakData/urls.py有:

from django.conf.urls import patterns, include, url
from django.contrib import admin
admin.autodiscover()

from showCrime import views
urlpatterns = patterns('',
    url(r'^$', views.index, name='index'),
    url(r'^showCrime/', include('showCrime.urls')),
    url(r'^admin/', include(admin.site.urls)),
)

djOakData/showCrime/urls.py有:

from django.conf.urls import patterns, url
import views
urlpatterns = patterns('',
    url(r'^$', views.index, name='index'),
        url(r'^tstImage.JPG$', views.index, name='showImage'),
)

djOakData/showCrime/views.py有:

from django.http import HttpResponse 
from django.template import Context, loader

def index(request):
    template = loader.get_template('showCrime/index.html')
    context = context = Context({})
    return HttpResponse(template.render(context))

def showImage(request):
    imagePath = "/Users/rik/Code/eclipse/djOakData/showCrime/tstImage.JPG"
    from PIL import Image
    Image.init()
    i = Image.open(imagePath)
    response = HttpResponse(mimetype='image/jpg')
    i.save(response,'JPG')
    return response

然后我index.html的就是这个:

<h1>ShowCrime Index</h1>
<img src="/showCrime/tstImage.JPG" width="500px">

(也尝试离开/showCrime; 仍然不行。)

我尝试了很多其他变体。他们中的许多人会抛出 404 错误,但这个版本不会。相反,我只是在日志中得到我期望的那种消息,要么尝试通过 index.html 引用图像,要么尝试直接指向它:

[11/May/2013 13:50:44] "GET /showCrime/tstImage.JPG HTTP/1.1" 200 391
[11/May/2013 13:50:44] "GET /showCrime/ HTTP/1.1" 200 391

关于我在做什么新事物的任何猜测?谢谢你的帮助。

4

1 回答 1

0

您的 tstImage 网址指向索引视图,而不是 showImage 之一。

于 2013-05-11T19:18:34.860 回答