2017-10-20 3 views
1

Je vais avoir du mal à obtenir des images à télécharger sur un seau S3 de Djangoobjet « InMemoryUploadedFile » n'a pas d'attribut « get »

L'erreur que je reçois est:

objet « InMemoryUploadedFile » n'a pas d'attribut « get »

J'ai regardé des questions similaires, mais ne l'ai pas trouvé une solution d'eux (ou non mentionnées ci-dessous):

ModelForm has no attribute get

Django image uploading

Voici mon code:

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) 

Merci beaucoup d'avance pour toute aide.

retraçage

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' 
+1

Avez-vous une trace complète? En regardant le retraçage, vous pointez généralement beaucoup plus rapidement dans la bonne direction. – Iguananaut

+0

Il ne semble pas y avoir de code ici concernant le téléchargement vers S3. –

+0

Je ne pense pas que le problème se produise vraiment directement à S3 mais c'est pourquoi nous devrions voir le * full * traceback. – Iguananaut

Répondre

1

Vous ne devriez pas passer un fichier spécifique de la demande à la forme, vous devez passer tout le dictionnaire FILES comme vous passez tout le POST.

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

Merci beaucoup! Cela a fonctionné parfaitement. –