Django:从Image url中添加ImageField中的图像 [英] Django: add image in an ImageField from image url

查看:121
本文介绍了Django:从Image url中添加ImageField中的图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请原谅我为我的丑陋的英语; - )



想象这个非常简单的模型:

  class photo(models.Model):
image = models.ImageField('Label',upload_to ='path /')
pre>

我想从图片网址创建照片(即不是在django管理员网站中手动)。



我认为我需要这样做:

  from myapp.models import照片
import urllib

img_url ='http://www.site.com/image.jpg'
img = urllib.urlopen(img_url)
#这里我需要检索图像(就像我把它放在管理员网站的输入中一样)
photo = Photo.objects.create(image = image)

我希望我已经很好地解释了这个问题,如果没有告诉我。



谢谢: p>

编辑:



这可能会工作,但我不知道如何转换 content 到django文件:

  from urlparse import urlparse 
import urllib2
from django.core.files import文件

photo =照片()
img_url ='http://i.ytimg.com/vi/GPpN5YUNDeI/default.jpg'
name = urlparse(img_url).path.split('/')[ - 1 ]
content = urllib2.urlopen(img_url).read()

#问题:内容必须是File
的一个实例photo.image.save(名称,内容,保存= True)


解决方案

我刚刚创建了 http://www.djangosnippets.org/snippets/1890/ 针对同样的问题。该代码类似于上面的pithyless'answer,除了它使用urllib2.urlopen,因为urllib.urlretrieve默认情况下不执行任何错误处理,所以很容易得到404/500页面的内容,而不是您需要的内容。你可以创建回调函数&自定义URLOpener子类,但是我发现它更容易创建我自己的临时文件,如下所示:

 从django.core.files导入文件
from django.core.files.temp import NamedTemporaryFile

img_temp = NamedTemporaryFile(delete = True)
img_temp.write(urllib2.urlopen(url).read())
img_temp.flush()

im.file.save(img_filename,File(img_temp))


please excuse me for my ugly english ;-)

Imagine this very simple model :

class Photo(models.Model):
    image = models.ImageField('Label', upload_to='path/')

I would like to create a Photo from an image URL (i.e., not by hand in the django admin site).

I think that I need to do something like this :

from myapp.models import Photo
import urllib

img_url = 'http://www.site.com/image.jpg'
img = urllib.urlopen(img_url)
# Here I need to retrieve the image (as the same way that if I put it in an input from admin site)
photo = Photo.objects.create(image=image)

I hope that I've well explained the problem, if not tell me.

Thank you :)

Edit :

This may work but I don't know how to convert content to a django File :

from urlparse import urlparse
import urllib2
from django.core.files import File

photo = Photo()
img_url = 'http://i.ytimg.com/vi/GPpN5YUNDeI/default.jpg'
name = urlparse(img_url).path.split('/')[-1]
content = urllib2.urlopen(img_url).read()

# problem: content must be an instance of File
photo.image.save(name, content, save=True)

解决方案

I just created http://www.djangosnippets.org/snippets/1890/ for this same problem. The code is similar to pithyless' answer above except it uses urllib2.urlopen because urllib.urlretrieve doesn't perform any error handling by default so it's easy to get the contents of a 404/500 page instead of what you needed. You can create callback function & custom URLOpener subclass but I found it easier just to create my own temp file like this:

from django.core.files import File
from django.core.files.temp import NamedTemporaryFile

img_temp = NamedTemporaryFile(delete=True)
img_temp.write(urllib2.urlopen(url).read())
img_temp.flush()

im.file.save(img_filename, File(img_temp))

这篇关于Django:从Image url中添加ImageField中的图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆