Wednesday 27 May 2020

Django - Can't get Images to upload in custom Class based form




In my models.py I have the following

class Board(models.Model):
title = models.CharField(max_length=100)
image = models.ImageField(default='default-sb.jpg', upload_to='surfboard_pics')

The field is called in the Class based form 

class BoardCreateView(LoginRequiredMixin, CreateView):
model = Board
fields = ['title', 'image', 'length', 'width', 'volume', 'wave_range_start',
'wave_range_start', 'wave_range_end', 'shaper', 'year', 'make', 'notes']

At this point I can see the field.  The image upload works in Admin but not in my Form !



The fix was this .  In the template you need 'multipart enctype' to the Form tag.  Like this 

<form method="POST" enctype="multipart/form-data">

After adding this my form now works. 











No comments: