为什么我的url匹配视图方法中的本地图像引用不是通过呈现?

时间:2013-05-11 19:12:15

标签: django

我通过django教程非常好,并且感觉非常好。

然后我开始尝试使用these suggestions for integrating 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

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

1 个答案:

答案 0 :(得分:0)

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