image upload and CreateView based view
Asked Answered
B

1

6

I want to be able to upload an image file using CreateView and a ModelForm but I can't get it working - it seems the form doesn't bind any file data after choosing a file. Here's the current content of the view:

class AddContentForm(forms.ModelForm):
    class Meta:
        model = Media


class AddContentView(CreateView):
    template_name = 'simple_admin/add_content.html'
    form_class = AddContentForm

    def get_success_url(self):
        return u'/opettajat/subcategory/{0}/{1}/'.format(self.kwargs['subcat_name'].decode('utf-8'), self.kwargs['subcat_id'].decode('utf-8'))


    def form_valid(self, form):
        isvalid = super(AddContentView, self).form_valid(form)
        s = Subcategory.objects.get(pk=self.kwargs['subcat_id'].encode('utf-8'))
        if self.request.POST.get('image'):
            image = form.cleaned_data['image']
            title = form.cleaned_data['art_title'].encode('utf-8')
            year_of_creation = form.cleaned_data['year_of_creation']
            m = Media.objects.get_or_create(image=image, art_title=title, year_of_creation=year_of_creation)[0]
            s.media.add(m)
            s.save()
       return isvalid

    def get_context_data(self, **kwargs):
        context = super(AddContentView, self).get_context_data(**kwargs)
        context['subcategory_name'] = self.kwargs['subcat_name'].encode('utf-8')
        context['subcategory_id'] = self.kwargs['subcat_id'].encode('utf-8')
        return context

     @method_decorator(login_required)
     def dispatch(self, request, *args, **kwargs):
        return super(AddContentView, self).dispatch(request, *args, **kwargs)

Can anyone help? An simple example of class based image upload view would be appreciated.

Beardsley answered 29/5, 2012 at 10:12 Comment(0)
R
19

Uploaded files are stored in request.FILES, not in request.POST. And don't forget to add enctype="multipart/form-data" to your <form> tag.

And I think form_valid method is for validation, not for data saving, isn't it?

Reduced answered 29/5, 2012 at 10:50 Comment(8)
I've have enctype="multipart/form-data" in my <form> (googled that one out just moment's ago) but thank you for the reminder :) self.request.POST.get('image') only checks if the forms image field was used. The big problem problem is that self.request.FILES doesn't contain anything when submitting the form so I can't do anything with it at all.Beardsley
@Beardsley Are files sent to the server? (check it with FireBug)Reduced
ah sorry - didn't notice the part about firebug - where in firebug can i find the info about sending files - I'm not to familiar with it. <form enctype="multipart/form-data" action="/opettajat/add/content/{{ subcategory_name }}/{{ subcategory_id }}/" method="post" style="float: none;">{% csrf_token %} {{ form.as_django_admin }} <input type="submit" value="Add Content"/> </form>Beardsley
@Beardsley On Network tab you can examine headers of your post request. If the file isn't sent, something is wrong with html layout of your form.Reduced
you were right - an error in the template file - it now uploads correctly - now I only need to fix some other stuff. Thank you for your help :)Beardsley
@Reduced to answer your question, form_valid is for saving. Validation is done by is_valid. ;)Peeve
@Peeve I was speaking about the method of CBV, not form's: docs.djangoproject.com/en/dev/ref/class-based-views/…Reduced
just adding enctype="multipart/form-data" to my form helped get things working for me, thanksOperand

© 2022 - 2024 — McMap. All rights reserved.