gpt4 book ai didi

python - “InMemoryUploadedFile”对象没有属性 'get'

转载 作者:太空宇宙 更新时间:2023-11-03 14:37:03 32 4
gpt4 key购买 nike

我在将图像从 Django 上传到 S3 存储桶时遇到问题

我收到的错误是:

'InMemoryUploadedFile' object has no attribute 'get'

我看过类似的问题,但没有找到解决方案(或下面未列出的其他问题):

ModelForm has no attribute get

Django image uploading

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'

最佳答案

您不应该将特定文件从请求传递到表单,您应该传递整个 FILES 字典,就像传递整个 POST 一样。

form = ImageForm(request.POST, request.FILES)

关于python - “InMemoryUploadedFile”对象没有属性 'get',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46857179/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com