I have created the following Django UpdateView:
class ProfileSettingsView(SuccessMessageMixin, UpdateView):
template_name = "accounts/settings/profile_settings.html"
form_class = ProfileSettingsForm
success_message = "Your profile has been successfully updated."
success_url = reverse_lazy("accounts:profile_settings")
context_object_name = "staff_member"
def get_object(self, queryset=None):
staff_member = get_object_or_404(StaffMember, user=self.request.user)
return staff_member
The ProfileSettingsForm is defined as follows:
class ProfileSettingsForm(forms.ModelForm):
class Meta:
model = StaffMember
fields = ["profile_image"]
And finally, the StaffMember model looks like this:
class StaffMember(models.Model):
user = models.OneToOneField(
"auth.User", on_delete=models.PROTECT, related_name="staff_member"
)
profile_image = models.ImageField(
upload_to=get_profile_image_upload_path,
blank=True,
default="none.png",
help_text="A profile image for this staff member. Must be square, and be in .png format.",
validators=[FileExtensionValidator(["png"]), validate_img_square],
)
My UpdateView renders correctly.
I select an image from my local machine and press submit.
The form posts successfully, and I see the success message. However, the validators, as defined on the StaffMember profile_image
field have not been run.
Also, the profile_image
field is not updated.
Why is validation not being called on my form fields, and why are they not being updated? What am I missing?