在Django中使用Amazon S3通过ImageField上传图片

5

我使用pip安装了django-storages (pip install django-storages)

获取了AWS首选项的设置:

DEFAULT_FILE_STORAGE = 'storages.backends.s3.S3Storage'
AWS_STORAGE_BUCKET_NAME = 'bar.media'
AWS_ACCESS_KEY_ID = 'MyAwesomeKeyId'
AWS_SECRET_ACCESS_KEY = 'BlahBlahBlah'

我從這樣得到我的模型:
def Bar(Model):
   image = models.ImageField(upload_to='bar')

在 forms.py 文件中:
 class BarForm(forms.Form):
     image = forms.FileField(label='Logo',required=False)
     def __init__(self, *args, **kwargs):
            super(BarForm, self).__init__(*args,**kwargs)
            self.fields['image'].label='My Awesome Photo'

在views.py文件中:

     @csrf_exempt
     def bar_web(request):
         context = {}
         if request.method == 'POST':
            form = BarForm(request.POST, request.FILES)
            context['form'] = form
            if form.is_valid():
                    try:
                            logoFile = request.FILES['image']
                            print 'PIC: '+str(logoFile)
                    except:
                            logoFile = False 
                    if logoFile:
                          bar = Bar(image=logoFile)
                          bar.save()
                          bar.reload()

在模板上:

         <p>Image Name: {{bar.image.name}}</p>
         <p>Image Url: {{bar.image.url}}</p>
         <p>Image : {{bar.image.content_type}}</p>
         <p>Image Path: {{bar.image.path}}</p>

但是没有任何打印出来。

我看到目前为止只有文件名被打印了出来,但是s3上面什么都没有发生。没有文件被上传。

我没有看到任何错误信息,这是什么问题呢?


1
看看这个链接 http://tartarus.org/james/diary/2013/07/18/fun-with-django-storage-backends 可能会对你有所帮助。 - dhana
看起来很好,但没有帮助。同样的问题,我不知道我做错了什么。 - JorgeParada
1个回答

6

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接