我正在編寫一個Django應用程序,它將獲取特定URL的所有圖像並將它們保存在數據庫中。下載遠程圖像並將其保存到Django模型
但我沒有得到如何在Django中使用ImageField。
Settings.py
MEDIA_ROOT = os.path.join(PWD, "../downloads/")
# URL that handles the media served from MEDIA_ROOT. Make sure to use a
# trailing slash.
# Examples: "http://example.com/media/", "htp://media.example.com/"
MEDIA_URL = '/downloads/'
models.py
class images_data(models.Model):
image_id =models.IntegerField()
source_id = models.IntegerField()
image=models.ImageField(upload_to='images',null=True, blank=True)
text_ind=models.NullBooleanField()
prob=models.FloatField()
download_img.py
def spider(site):
PWD = os.path.dirname(os.path.realpath(__file__))
#site="http://en.wikipedia.org/wiki/Pune"
hdr= {'User-Agent': 'Mozilla/5.0'}
outfolder=os.path.join(PWD, "../downloads")
#outfolder="/home/mayank/Desktop/dreamport/downloads"
print "MAYANK:"+outfolder
req = urllib2.Request(site,headers=hdr)
page = urllib2.urlopen(req)
soup =bs(page)
tag_image=soup.findAll("img")
count=1;
for image in tag_image:
print "Image: %(src)s" % image
filename = image["src"].split("/")[-1]
outpath = os.path.join(outfolder, filename)
urlretrieve('http:'+image["src"], outpath)
im = img(image_id=count,source_id=1,image=outpath,text_ind=None,prob=0)
im.save()
count=count+1
我打電話download_imgs.py一個視圖中像
if form.is_valid():
url = form.cleaned_data['url']
spider(url)
https://docs.djangoproject.com/en/dev/ref/models/fields/#django.db.models.ImageField – 2013-04-23 16:11:23
你想將圖像保存在數據庫斑點或圖像路徑 – mossplix 2013-04-23 16:38:29
@ mossplix - 無論哪種方式...但是,如果我保存圖像作爲路徑,那麼我也想在我的服務器上的圖像 – 2013-04-23 16:48:40