image upload and CreateView based view
django, django-class-based-views, image-upload
Solution
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?
Problem
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.