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

时间:2021-12-24 03:13:23

i'm pretty well thru the django tutorials, and was feeling pretty good about how it is going.

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

but then i started to try to use these suggestions for integrating matplotlib. a first step is to include a static image and that's what i'm trying to do. details:

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

my file hierachy looks like this:

我的文件层次如下:

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 has:

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 has:

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 has:

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

and then my index.html has just this in it:

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

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

(also tried leaving off the /showCrime; still no go.)

(也试过离开/ showCrime;仍然没有去。)

i've tried lots of other variants. many of them throw 404 errors, but this version does not. rather, i get just the sort of messages in the log i'd expect, either trying to reference the image via index.html or trying to point at it directly:

我尝试了很多其他的变种。他们中的许多人抛出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

any guesses as to what newby thing i'm doing? thanks for your help.

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

1 个解决方案

#1


0  

Your tstImage url is pointing to the index view, not the showImage one.

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

#1


0  

Your tstImage url is pointing to the index view, not the showImage one.

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