我正在编写使用ImageField的我的第一个django应用程序,并且我有 困难。问题是我的图片没有上传。我看过 尽可能多的例子,我可以找到。我不确定这里有什么问题。无法使用ImageField在django上传图像
我试图通过查看upload_to
目录的位置 来验证我的照片正在上传。当表单显示在网页上时,会显示 正确的上传文件按钮。当我点击提交时,下面的代码执行, ,但没有图像上传。
基础上upload_to
,我期待看到上传看到在任一图片图片: myproject/photos
或myproject/media/photos
是否正确?
我在这里做了什么明显的错误?我如何让我的图片上传?
-------------- settings.py -------------
MEDIA_ROOT = '/home/me/django_projects/myproject/media/'
MEDIA_URL = '/media/'
--------- ----- model.py -------------
class Person(models.Model):
lastName = models.CharField(max_length=20)
firstName = models.CharField(max_length=20)
image = models.ImageField(upload_to='photos', blank=True, null=True)
# save executes but no image is saved.
# Because images are uploaded along with a new entry, I needed a special
# work around to get the self.id
def save(self):
for field in self._meta.fields:
if field.name == 'image':
if self.id is not None:
#save image now
field.upload_to = 'photos/%d' % self.id
else:
#Save image later
saveImageLast = True
super(Customer, self).save() # should create self.id if not already
if saveImageLast == True:
field.upload_to = 'photos/%d' % self.id
super(Customer, self).save()
print "save complete" #prints, but no image ...?
-------------- forms.py ----- --------
class PersonForm(ModelForm):
class Meta:
model = Person
fields = ('lastName', 'firstName', 'image')
我确信,我的形式包括ENCTYPE为“多/表单数据”。但仍然没有上传。 :( – codingJoe
你能否更新你的问题并添加你的views.py? –