views:

1097

answers:

4

Hi!

My problem is simple. I have template like this:

<form enctype="multipart/form-data"
action="{% url offers.views.add_offer %}" method="post">    
    <input type="file" name="image1" />
    <input type="file" name="image2" />  
    <input type="submit" value="Add" />
</form>

Model looks like that:

class Image(models.Model):
    image = models.ImageField(upload_to='uploads/images/offers/')

And forms like that (it uses model Image):

class ImageForm(ModelForm):
    class Meta:
        model = Image

And view like that:

    for f in request.FILES:
    # imageform:
        image = ImageForm(request.POST, f)
        image.save()

The problem is that I can't upload images. I want to save image in the two seperate instances od Image Model.
I have an error:

'unicode' object has no attribute 'get'

Thanks for any help and response.

Updated for provide more information

+1  A: 

What makes you think that would work? You're iterating through request.FILES and attempting to instantiate a form on each iteration passing a file object. That's nothing like what's in the documentation, which tells you to pass the whole of request.FILES in.

Edited after comment Look, you haven't given us much information to go on. Does your model have one image or two? Why are you trying to handle the two images separately? Are you trying to create two separate model instances, or one with two images? What exactly are you trying to do?

Basically you just want to do this:

form = ImageForm(request.POST, request.FILES)
if form.is_valid():
    form.save()

and that's it.

Daniel Roseman
So what I must do? You have any solution for that?
galuszkak
Model looks like that: class Image(models.Model): image = models.ImageField(upload_to='uploads/images/offers/')It has only one ImageField. I i want create two seperatly instances of the same model. I know how to save one file/image. It's simple as you describe above. That is not problem for me. My problem is that i don't know how to create that two instances (retrieve files from request.FILES and then save it to two separated new instances). If you want more information I can post it. I want to save it to the same Model.
galuszkak
I updated the question for information that you were asking.
galuszkak
A: 

I make in views.py something like that:

for i in range(1, 2):
   image = ImageForm(request.POST, request.FILES['image%s' % i])

I have an error like that:

'InMemoryUploadedFile' object has no attribute 'get'

Anyone can help?

galuszkak
+2  A: 

Man, Django Formsets is what you need:

http://docs.djangoproject.com/en/dev/topics/forms/formsets/

Edited:

The view:

def manage_images(request):
    ImageFormSet = formset_factory(ImageForm)
    if request.method == 'POST':
        formset = ImageFormSet(request.POST, request.FILES)
        if formset.is_valid():
            # do something with the formset.cleaned_data
    else:
        formset = ImageFormSet()
    return render_to_response('manage_images.html', {'formset': formset})

The template:

<form enctype="multipart/form-data" action="{% url offers.views.add_offer %}" method="post">
    {{ formset.management_form }}
    <table>
        {% for form in formset.forms %}
        {{ form }}
        {% endfor %}
    </table>
</form>
nabucosound
But how I must use it? My problem is to get data from request.FILES i was thinking. Can you give me example how to save many files to the same model in many instnaces with formsets?
galuszkak
I have edited my answer to show you how
nabucosound
+1  A: 

here you'll find the docs for file upload.

I save my image in the form's save()-method like this:

def save(self): 
    if self.cleaned_data.get('galleryname'):
        if self.cleaned_data.get('images1'):

            path = 'images/'+ urlify(self.cleaned_data.get('galleryname'))+self.cleaned_data.get('images1').name
            destination = open(s.MEDIA_ROOT+path, 'wb+')
            for chunk in self.cleaned_data.get('images1').chunks():
                destination.write(chunk)
            p = Photo()
            p.picture="./"+path
            p.save()

and in the view I have

form = CompleteSubscriptionForm(request.POST, request.FILES, error_class=DivErrorList)
if form.is_valid(): # All validation rules pass
        form.save()
vikingosegundo