Aargh. Thanks. On Saturday, July 2, 2016, Vijay Khemlani <[email protected]> wrote:
> The typical problem is forgetting to add the > > enctype='multipart/form-data' > > attribute to the form tag > > > > On Sat, Jul 2, 2016 at 9:40 PM, Lee Hinde <[email protected] > <javascript:_e(%7B%7D,'cvml','[email protected]');>> wrote: > >> Using django-storages to put user uploaded files on s3. >> >> Uploading images in the shell works fine. So, I assume I've got >> django-storages and boto installed fine and S3 configured correctly. >> >> Uploading in real-life, doesn't work. The error I get back is that the >> image field is required. >> >> The model: >> >> class PropertyPhotos(models.Model): >> property = models.ForeignKey(Property) >> image = models.ImageField(upload_to="img") >> title = models.CharField(max_length=200, blank=True) >> >> The form: >> >> class PhotoForm(forms.ModelForm): >> property_key = forms.CharField(required=False, >> widget=forms.HiddenInput()) >> >> def __init__(self, property_key, *args, **kwargs): >> super(PhotoForm, self).__init__(*args, **kwargs) >> self.initial['property_key'] = property_key >> >> class Meta: >> model = PropertyPhotos >> fields = [ 'image', 'title'] >> exclude = ['property', ] >> >> >> The view: >> >> class AddPhotos(LoginRequiredMixin, CreateView): >> form_class = PhotoForm >> success_url = reverse_lazy('add_photos') >> template_name = 'photo_entry.html' >> model = PropertyPhotos >> >> def get_form_kwargs(self): >> kwargs = super(AddPhotos, self).get_form_kwargs() >> kwargs['property_key'] = self.kwargs.get('key') >> return kwargs >> >> def form_valid(self, form): >> photo = PhotoForm(self.request.POST, self.request.FILES) # >> form.save(commit=False) >> url = form.cleaned_data['property_key'] >> photo.property = Property.objects.get(url_uuid=url) >> photo.save() >> return super(AddPhotos, self).form_valid(form) >> >> def get_context_data(self, **kwargs): >> context = super(AddPhotos, self).get_context_data(**kwargs) >> context['property_key'] = self.kwargs.get('key') >> context['property_obj'] = >> Property.objects.get(url_uuid=self.kwargs.get('key')) >> return context >> >> The form part of the template (mostly to show that the image input is >> within the form : >> >> <form method="post" id="upload-form" action="." class="responsive-form >> card row"> >> {{ form.errors }} >> {% csrf_token %} >> {{ form.property_key }} >> <h1>Add Photos for - {{ property_obj.title }}</h1> >> {{ form.as_p }} >> >> <div class="col-md-12 form-option form-actions"> >> <div class="col-md-12"> >> <button name="submit" class="btn btn-default btn-block" >> value="Upload" id="submit" type="submit"> >> Upload >> </button> >> </div> >> >> </div> >> </form> >> >> I'd appreciate a code review that will tell me which mis-placed >> semi-colon is causing my errors. >> >> Thanks! >> >> > -- You received this message because you are subscribed to the Google Groups "Django users" group. To unsubscribe from this group and stop receiving emails from it, send an email to [email protected]. To post to this group, send email to [email protected]. Visit this group at https://groups.google.com/group/django-users. To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/CA%2BePoMyipqzzD-yvA3k4JOn0YG4sDD20VYS2Jm9E509WFWOMXQ%40mail.gmail.com. For more options, visit https://groups.google.com/d/optout.

