django PIL图片上传/显示问题

时间:2022-04-24 00:24:56

I am trying to get django to show pictures. Ive installed PIL and it seams to work. i am running py 2.7, win 7, the developement server and sqlite3

我想让django来展示照片。我已经安装了PIL并且接缝正常工作。我正在运行py 2.7,win 7,开发服务器和sqlite3

my static root:

我的静态根:

STATIC_ROOT = r'H:/netz2/skateproject/static/'

ive got a background picture in this folder and can acces it via

我在这个文件夹中有一张背景图片,可以通过它访问它

http://127.0.0.1:8000/static/images/lomax.jpg

now im trying to upload a pic via an image field & the admin. it works fine, the picture lands in the right folder (ive changed the folders a million times by now to see if that could be the problem) but the picture can not be displayed.

现在我试图通过图像字段和管理员上传图片。它工作正常,图片落在正确的文件夹(香港专业教育学院已经改变文件夹一百万次,看看是否可能是问题)但图片无法显示。

part of my model:

我模特的一部分:

class Sk8(models.Model):
    name        = models.CharField(max_length=50, default='name of the model')
    bild1       = models.ImageField(upload_to="uploads/")

my media root:

我的媒体根:

MEDIA_ROOT = r'H:/netz2/skateproject/media/'

the picture lands in the right folder. netz/skateproject/media/pic.jpg

图片落在正确的文件夹中。 NETZ / skateproject /媒体/ pic.jpg

but it seams like localhost can not acces this folder. when i try - i get an 404 and django tells me that the url does not match anything i defined in my urls. which is true - but neither did I define a url for the background image in the static folder - and i can access this one just fine.

但它像localhost这样的接缝无法访问此文件夹。当我尝试 - 我得到一个404和django告诉我,网址与我在我的网址中定义的任何内容都不匹配。这是真的 - 但我也没有为静态文件夹中的背景图像定义一个网址 - 我可以很好地访问这个。

ive also tried to acces the file from my html template, like this:

香港专业教育学院也尝试从我的HTML模板访问该文件,如下所示:

{% extends "grundgerüst.html" %}
{% block inhalt %}
    <div id='skatelist'>
        {% for sk8 in skates %}
        <p><a href="/skates/{{sk8.slug}}/">{{sk8}}</a></p>
        <p><img src="{{sk8.bild.url}}"/></p>
        {% endfor %}
    </div>
{% endblock %}

obviously the picture does not show up.

很明显,图片没有显示出来。

I dont know what is wrong here. i guess i shoudl be able to see the pic on localhost? heres a pic of the error chrome gives me

我不知道这里有什么问题。我想我可以在localhost上看到这张照片吗?继承人错误铬的照片给了我

django PIL图片上传/显示问题

if i.e. in the picture the folders are different than the ones in the code example its because i changed them testing different settings.

如果在图片中文件夹与代码示例中的文件夹不同,那是因为我改变了它们测试不同的设置。

I really hope somebody can help me to figure out image_field, cause uploading pictures if i cant display them does not make much sense :)

我真的希望有人可以帮我弄清楚image_field,如果我不能显示它们上传图片没有多大意义:)

thanks in advance!!! danielll

提前致谢!!! danielll

ouh and - i already tried google and django reference etc. i honestly could not figure it out that way. every help appreciated!

ouh和 - 我已经尝试过google和django参考等。老实说,我无法弄明白这一点。每一个帮助赞赏!

1 个解决方案

#1


1  

Django's contrib.staticfiles app will monkeypatch your urls to serve static files when in DEBUG mode [ref]. Unfortunately the same cannot be said of media files.

在DEBUG模式下,Django的contrib.staticfiles应用程序将monkeypatch你的url服务静态文件[参考]。不幸的是,媒体文件也不能说。

Add the following snippet to your project urls.py to display media

将以下代码段添加到项目urls.py以显示媒体

django.conf import settings

if settings.DEBUG:
    urlpatterns += patterns('',
        url(r'^media/(?P<path>.*)$', 'django.views.static.serve', {
            'document_root': settings.MEDIA_ROOT,
        }),
   )

Docs reference

#1


1  

Django's contrib.staticfiles app will monkeypatch your urls to serve static files when in DEBUG mode [ref]. Unfortunately the same cannot be said of media files.

在DEBUG模式下,Django的contrib.staticfiles应用程序将monkeypatch你的url服务静态文件[参考]。不幸的是,媒体文件也不能说。

Add the following snippet to your project urls.py to display media

将以下代码段添加到项目urls.py以显示媒体

django.conf import settings

if settings.DEBUG:
    urlpatterns += patterns('',
        url(r'^media/(?P<path>.*)$', 'django.views.static.serve', {
            'document_root': settings.MEDIA_ROOT,
        }),
   )

Docs reference