0

Hi I'm new to Django I'm trying to build a simple upload application.

forms.py

from django import forms

class Song(forms.Form):
song = forms.ImageField()

models.py

from django.db import models
class Song(models.Model):
song = models.ImageField(upload_to='songs')

views.py

def upload(request):

if request.method  == 'POST':
    form = Song(request.POST, request.FILES)
    if form.is_valid():
        handle_uploaded_file(request.FILES['song'])
        return HttpResponseRedirect('/done/')

else:
    form = Song()
    return render_to_response('upload.html', {
    'form': form},
    context_instance=RequestContext(request)
    )

def handle_uploaded_file(f):
    destination = open('/home/fh/Desktop/netz/media/name.txt', 'wb+')
    for chunk in f.chunks():
        destination.write(chunk)
    destination.close()

template

<form action="/upload/" enctype="multipart/form-data" method="post">{% csrf_token %}
{{ form.as_p }}
<input type="submit" value="Submit" />
</form>

When I test it I get this error. And I don' even know what that means. Can anyone figure out what I'm doing wrong?

AttributeError at /upload/

'InMemoryUploadedFile' object has no attribute 'COOKIES'

Request Method:     POST
Request URL:    http://localhost:8080/upload/
Django Version:     1.3.1
Exception Type:     AttributeError
Exception Value:    

'InMemoryUploadedFile' object has no attribute 'COOKIES'

Exception Location:     /usr/local/lib/python2.6/dist-packages/django/middleware  /csrf.py in process_view, line 116
Python Executable:  /usr/bin/python
Python Version:     2.6.6
Python Path:    

['/usr/lib/python2.6',
 '/usr/lib/python2.6/plat-linux2',
 '/usr/lib/python2.6/lib-tk',
 '/usr/lib/python2.6/lib-old',
 '/usr/lib/python2.6/lib-dynload',
 '/usr/local/lib/python2.6/dist-packages',
 '/usr/lib/python2.6/dist-packages',
 '/usr/lib/python2.6/dist-packages/PIL',
 '/usr/lib/python2.6/dist-packages/gst-0.10',
 '/usr/lib/pymodules/python2.6',
 '/usr/lib/pymodules/python2.6/gtk-2.0',
 '/home/fh/Desktop/netz/Klangmagnet',
 '/home/fh/Desktop/netz/Klangmagnet/Klangmagnet']

Server time:    Wed, 15 Feb 2012 19:51:30 -0600
LonnyT
  • 590
  • 1
  • 8
  • 21

1 Answers1

0

Check if you overwrite the queryset for the model in admin.py. Like:

def queryset(self, request):
    qs = super(MainClass, self).queryset(request)
    return qs.only( field1, field2,... ) 

Then, please make sure to include the image field in the queryset. Like:

def queryset(self, request):
    qs = super(MainClass, self).queryset(request)
    return qs.only( field1, field2,....,ImageFieldName ) 
Dave Clemmer
  • 3,741
  • 12
  • 49
  • 72