我无法将图片从Django上传到S3存储桶
我得到的错误是:
' InMemoryUploadedFile'对象没有属性' get'
我已经查看了类似的问题,但未找到他们(或下面未列出的其他人)的解决方案:
ModelForm has no attribute get
django 'User' object has no attribute 'get' error
这是我的代码:
forms.py
from django import forms
from .models import UploadImage, UploadAudio
class ImageForm(forms.ModelForm):
class Meta:
model = UploadImage
fields = ['myimage']
views.py
from django.contrib import messages
from django.shortcuts import render, get_object_or_404, redirect
from django.http import HttpResponse, HttpResponseRedirect
from django.shortcuts import redirect
from .forms import ImageForm, AudioForm
from .models import UploadImage
def ImageCreate(request):
if request.method == 'POST':
form = ImageForm(request.POST, request.FILES['myimage'])
if form.is_valid():
image = form.save(commit=False)
image.author = request.user
image.save()
messages.success(request, "Uploaded successfully")
return redirect('mytimer')
else:
messages.error(request, "Unable to upload at this time")
else:
form = ImageForm()
return render(request, "myimages.html", {'form': form})
models.py
from __future__ import unicode_literals
from django.db import models
from django.conf import settings
class UploadImage(models.Model):
"""
Define how the user will upload images
"""
# link author to registered user
author = models.ForeignKey(settings.AUTH_USER_MODEL)
myimage = models.ImageField(upload_to='myimages/')
uploaded_at = models.DateTimeField(auto_now_add=True)
非常感谢您提前寻求帮助。
回溯
Environment:
Request Method: POST
Request URL: http://127.0.0.1:8000/myimages/
Django Version: 1.11.6
Python Version: 2.7.12
Installed Applications:
['django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'hello',
'timer',
'accounts',
'django_forms_bootstrap',
'diary',
'storages',
'useruploads',
'debug_toolbar']
Installed Middleware:
['django.middleware.security.SecurityMiddleware',
'debug_toolbar.middleware.DebugToolbarMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware']
Traceback:
File
"/home/pauly/Documents/MeditationTimer/env/local/lib/python2.7/site-
packages/django/core/handlers/exception.py" in inner
41. response = get_response(request)
File
"/home/pauly/Documents/MeditationTimer/env/local/lib/python2.7/site-
packages/django/core/handlers/base.py" in _get_response
187. response =
self.process_exception_by_middleware(e,
request)
File
"/home/pauly/Documents/MeditationTimer/env/local/lib/python2.7/site-
packages/django/core/handlers/base.py" in _get_response
185. response = wrapped_callback(request,
*callback_args, **callback_kwargs)
File "/home/pauly/Documents/MeditationTimer/Meditate_prj/
useruploads/views.py" in ImageCreate
15. if form.is_valid():
File
"/home/pauly/Documents/MeditationTimer/env/local/lib/python2.7/site-
packages/django/forms/forms.py" in is_valid
183. return self.is_bound and not self.errors
File
"/home/pauly/Documents/MeditationTimer/env/local/lib/python2.7/site-
packages/django/forms/forms.py" in errors
175. self.full_clean()
File
"/home/pauly/Documents/MeditationTimer/env/local/lib/python2.7/site-
packages/django/forms/forms.py" in full_clean
384. self._clean_fields()
File
"/home/pauly/Documents/MeditationTimer/env/local/lib/python2.7/site-
packages/django/forms/forms.py" in _clean_fields
396. value =
field.widget.value_from_datadict(self.data, self.files,
self.add_prefix(name))
File
"/home/pauly/Documents/MeditationTimer/env/local/lib/python2.7/site-
packages/django/forms/widgets.py" in value_from_datadict
423. upload = super(ClearableFileInput,
self).value_from_datadict(data, files, name)
File
"/home/pauly/Documents/MeditationTimer/env/local/lib/python2.7/site-
packages/django/forms/widgets.py" in value_from_datadict
367. return files.get(name)
Exception Type: AttributeError at /myimages/
Exception Value: 'InMemoryUploadedFile' object has no attribute 'get'
答案 0 :(得分:1)
您不应该将请求中的特定文件传递给表单,您应该在传递整个POST时传递整个FILES字典。
UIViewController