The view should be like this, btw:

def photo_upload(request):
    form = PhotoUploadForm()
    if request.POST:
        form = PhotoUploadForm(request.user, request.POST,
request.FILES)
        if form.is_valid():
            form.save()
            json = {'status': 'success', 'message': _("Upload
succeeded.")}
            return JsonResponse(json)
    json = jsonify(form)
    return JsonResponse(json)

On 21 jun, 10:32, SanPy <[EMAIL PROTECTED]> wrote:
> Here are some code samples. Functionality may change in the future:
>
> class PhotoUploadForm(newforms.Form):
>     item_id     =
> newforms.IntegerField(widget=newforms.widgets.HiddenInput)
>     photo       = newforms.ImageField(label=_('photo'),
> widget=newforms.widgets.FileInput)
>
>     def __init__(self, user, *args, **kwargs):
>         super(PhotoUploadForm, self).__init__(*args, **kwargs)
>         self.user = user
>         self.item = None
>
>     def clean_item_id(self):
>         item_id = int(self.cleaned_data.get('item_id'))
>         try:
>             self.item = Item.on_site.get(pk=item_id)
>         except Item.DoesNotExist:
>             raise newforms.ValidationError(_("Invalid input."))
>         return item_id
>
>     def clean(self):
>         if self.item.user != self.user:
>             raise newforms.ValidationError(_("You are not allowed to
> upload photos for this item."))
>         nr_of_images = self.item.photo_set.count()
>         if nr_of_images >= MAXIMUM_IMAGES_PER_ITEM:
>             raise newforms.ValidationError(_("Maximum number of photos
> reached for this item."))
>         return self.cleaned_data
>
>     def save(self):
>         photo = Photo()
>         photo.item = self.item
>         if not self.item.photo_set.filter(**{'main' : True}):
>             photo.main = True
>         uploaded_file = self.cleaned_data['photo']
>         content = uploaded_file.content
>         photo.save_image_file(uploaded_file.filename, content)
>         photo.size = len(content)
>         photo.save()
>
> And the view (replace JsonResponse with HttpResponse). JsonResponse
> and jsonify are custom, not included in the django source:
>
> def photo_upload(request):
>     if request.POST:
>         form = PhotoUploadForm(request.user, request.POST,
> request.FILES)
>         if form.is_valid():
>             form.save()
>             json = {'status': 'success', 'message': _("Upload
> succeeded.")}
>             return JsonResponse(json)
>     form = PhotoUploadForm()
>     json = jsonify(form)
>     return JsonResponse(json)
>
> Regards, Sander.
>
> On 20 jun, 22:19, SanPy <[EMAIL PROTECTED]> wrote:
>
> > Dirk,
>
> > File and image uploading for newforms, AFAIK, is not implemented in
> > 0.96. There is a ticket for this (#3297). If you look in the django-
> > developers newsgroup and search for #3297, you will find more
> > information. It is possible to have it working with the latest trunk
> > from SVN and a patch from #3297. It works fine on my machine with the
> > latest trunk and patch.
>
> > Als je er niet uitkomt, laat het hier dan even weten, dan geef ik je
> > wat code voorbeelden.
>
> > Groet, Sander.
>
> > On 20 jun, 18:15, "Dirk van Oosterbosch, IR labs" <[EMAIL PROTECTED]>
> > wrote:
>
> > > Hello,
>
> > > I'm starting with Django, so I was adviced to use the newforms. But I
> > > can't just figure out how to correctly upload images (or other files
> > > for that matter). I heard that there was already much discussion on
> > > this list, but on groups.google.com I couldn't find any pointers.
>
> > > I tried out two code examples from blogs, but get stuck with both.
>
> > > First I tried this 
> > > one:http://www.oluyede.org/blog/2007/03/18/django-image-uploading-
> > > validation-and-newforms/
> > > The strange thing about this example is that part of the validation
> > > happens in the view (not in the clean_photo method of the Form).
> > > I got stuck with this, because this (line 6 , in views):
> > >                  img = Image.open(StringIO(request.FILES['photo']
> > > ['content']))
> > > kept failing. I would like to test this behavior of Image in the
> > > django shell, but wouldn't know how.
>
> > > Then I triedhttp://batiste.dosimple.ch/blog/2007-05-13-1/
> > > which claims to be based on the former example, but seems quite
> > > different to me. Probably the reason why I can't get this to work is
> > > my lack of understanding the monkey-patching business.
> > > The strange thing here (i.e. the part where I get lost), is that the
> > > clean_avatar method takes two arguments clean_avatar(self, value),
> > > but I have no idea where this other argument should be coming from.
> > > (Django has the same problem: TypeError at /upload/  clean_photo()
> > > takes exactly 2 arguments (1 given))
>
> > > I'm using 0.96
>
> > > I was hoping you guys could lead me towards working image upload. ;-)
>
> > > best
> > > dirk
>
> > > -----------------------------
> > > Dirk van Oosterbosch
> > > de Wittenstraat 225
> > > 1052 AT Amsterdam
> > > the Netherlands
>
> > >http://labs.ixopusada.com
> > > -----------------------------


--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to