33

我有models.py

class Item(models.Model):
    image_file = models.ImageField(upload_to='images')
    image_url = models.URLField()

我希望 django 自动下载并在本地保存图像image_url并“连接”它image_file

它应该如何行动:

  1. 我粘贴https://docs.djangoproject.com/s/img/site/hdr_logo.gifimage_url管理员的字段中
  2. 点击“保存”
  3. 在模板中写<img src="{{ item.image_file.url }}">. 它显示来自我的服务器的图像,而不是djangoproject.com

我试过的:

我已经覆盖saveItem课堂方法。我通过本地保存图像urllib,但我坚持将此保存的图像与image_file字段连接

4

4 回答 4

46
from django.core.files import File
import os

class Item(models.Model):
    image_file = models.ImageField(upload_to='images')
    image_url = models.URLField()

...

def get_remote_image(self):
    if self.image_url and not self.image_file:
        result = urllib.urlretrieve(self.image_url)
        self.image_file.save(
                os.path.basename(self.image_url),
                File(open(result[0]))
                )
        self.save()

您可以覆盖默认的 save() 方法以自动调用 get_remote_image()。

请参阅:https ://docs.djangoproject.com/en/dev/topics/db/models/#overriding-model-methods

于 2013-05-05T09:24:24.033 回答
13
from django.db import models
from django.core.files import File
from urllib.request import urlopen
from tempfile import NamedTemporaryFile

class Item(models.Model):
    image_file = models.ImageField(upload_to='images')
    image_url = models.URLField()

...

def get_remote_image(self):
    if self.image_url and not self.image_file:
        img_temp = NamedTemporaryFile(delete=True)
        img_temp.write(urlopen(self.image_url).read())
        img_temp.flush()
        self.image_file.save(f"image_{self.pk}", File(img_temp))
    self.save()

此解决方案避免在 url 过程中收到任何 utf-8 错误。

它仅适用于 python 3.6+,因为 f 字符串。

检查此链接:https ://twigstechtips.blogspot.com/2012/04/django-programmatically-saving-image.html

于 2018-10-07T16:01:27.220 回答
8

对于python3

from django.core.files import File
from urllib import request
import os

class Item(models.Model):
    image_file = models.ImageField(upload_to='images')
    image_url = models.URLField()

...

def get_remote_image(self):
    if self.image_url and not self.image_file:
        result = request.urlretrieve(self.image_url)
        self.image_file.save(
                os.path.basename(self.image_url),
                File(open(result[0], 'rb'))
                )
        self.save()

从这里:http ://stackoverflow.com/questions/17960942/attributeerror-module-object-has-no-attribute-urlretrieve

于 2017-12-27T23:17:05.533 回答
3

Python3

from django.db import models
from django.core.files import File
from urllib.request import urlopen
from tempfile import NamedTemporaryFile

class Item(models.Model):
    image_file = models.ImageField(upload_to='images')
    image_url = models.URLField()

    def save(self, *args, **kwargs):
        if self.image_url and not self.image_file:
            img_temp = NamedTemporaryFile(delete=True)
            img_temp.write(urlopen(self.image_url).read())
            img_temp.flush()
            self.image_file.save(f"image_{self.pk}", File(img_temp))
        super(Item, self).save(*args, **kwargs)

类似,但在添加“image_url”时自动保存“image_file”

于 2019-04-16T19:33:50.220 回答