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!