0

I want to upload a in image in database , but after reading tons of tutorials , this code doesn't work because of getting this error after printing form.errors:

**AttributeError at /upload**
'ImageUploadForm' object has no attribute 'cleaned_data'

here are my codes :
models.py:

class room_type(models.Model):
    id = models.IntegerField(primary_key = True)
    code = models.CharField(max_length = 40)
    name = models.CharField(max_length= 40 )
    title = models.CharField(max_length = 40)
    english_title = models.CharField(max_length=40)
    capacity = models.IntegerField()
    extra_capacity = models.IntegerField()
    description = models.CharField(max_length=255)
    today_price = models.IntegerField()

    class Meta:
        db_table = 'room_types'
    def __unicode__(self):
        return u'%d' % (self.id)

class attachment(models.Model):

    id = models.IntegerField(primary_key = True)
    image = models.ImageField(upload_to="/home/iman/Desktop/code/hotel/rooms/attachments")

    #foreign key : a room has many images
    rt_id = models.ForeignKey(room_type)
    class Meta:
        db_table = 'attachments'
    def __unicode__(self):
        return u'%d' % (self.id)

forms.py:

  class ImageUploadForm(forms.Form):
        image = forms.ImageField()
        class Meta:
        model = attachment
        fields= ['image']

views.py:

def upload(request):
    if request.method == 'POST':
        print"##########"
        form = ImageUploadForm(request.POST,request.FILES)
        if form.is_valid():
            new_attachment = attachment()
            new_attachment.image= form.cleaned_data['image']
            new_attachment.id = 1
            new_attachment.rt_id = 1
            new_attachment.save()
            print "))))))))"
            #return HttpResponse('image upload success')
        else:
            print form.is_valid() 
            print form.errors
            form = ImageUploadForm()
    return render(request,'index.html') 

index.html :

<div id="dialog" title="آپلود فایل">
            <form action="{% url 'upload' %}" class="dropzone" enctype="multipart/form-data"  id="my-awesome-dropzone" method="post"  >{% csrf_token %}
                  <input id="id_image" type="file" class="" name="image">
                  <input type="submit" value="Submit" />
            </form>
        </div>


> full traceback :  Environment:
> 
> 
> Request Method: POST Request URL: http://localhost:8000/upload
> 
> Django Version: 1.6.5 Python Version: 2.7.3 Installed Applications:
> ('django.contrib.admin',  'django.contrib.auth', 
> 'django.contrib.contenttypes',  'django.contrib.sessions', 
> 'django.contrib.messages',  'django.contrib.staticfiles',  'rooms')
> Installed Middleware:
> ('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
> "/usr/local/lib/python2.7/dist-packages/django/core/handlers/base.py"
> in get_response
>   112.                     response = wrapped_callback(request, *callback_args, **callback_kwargs) File "/home/iman/Desktop/code/hotel/rooms/views.py" in upload
>   147.             print form.cleaned_data['image']
> 
> Exception Type: AttributeError at /upload Exception Value:
> 'ImageUploadForm' object has no attribute 'cleaned_data'
4
  • Your form in the index.html is missing the form fields. Commented Aug 3, 2014 at 19:49
  • no , look at this link plz : startutorial.com/articles/view/… and this link:infotuts.com/… Commented Aug 3, 2014 at 19:55
  • I think dropzone works with ajax.. so you need to write logic which handle ajax call with csrf token Commented Aug 3, 2014 at 20:08
  • I am not sure but I think somewhere in the code, you have called form.cleaned_data before calling form.is_valid() (after reading the traceback) Commented Aug 4, 2014 at 9:15

1 Answer 1

1

Try removing

class Meta:
    model = attachment
Sign up to request clarification or add additional context in comments.

1 Comment

is there and input field with name=image in your form ?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.