I think there’s possibly a way around this with javascript, but my knowledge is very poor on that.
I’m making a function in my form class which examines a condition between two values and returns ValidationError if one value is less than the other. In other words - if the number input on the form is less than the number on that field in the first database record, then the validation error message should show (on the update page).
then I’m trying to override the Post method in the UpdateView class, the reason why I’m overriding post is because I’m also allowing the user to update Images (uploaded from an ImageField)
I’m trying to call this form function before form.save() happens, but it isn’t really working and I don’t know where to go.
if form.is_valid():
# form.clean()
f = form.save(commit=False)
f.user = request.user
f.save()
ValueError: The view dashboard.views.view didn’t return an HttpResponse object. It returned None instead.
but I don’t get this if I completely remove the clean method, I made sure the view is working correctly before trying this field validation action (and the numbers also print correctly inside clean(), it’s just that when I use this forms.ValidationError in the code - everything bombs)
here is the view (ignore the very experimental image update code):
class RecordUpdatePage(StaffMemberRequiredMixin, LoginRequiredMixin , UpdateView):
model = DashboardModel
template_name = "dashboard/record_update.html"
form_class = RecordForm
success_url = reverse_lazy("dashboard")
context_object_name = 'record'
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
context['imageform'] = ImageForm
return context
def post(self, request, *args, **kwargs):
form_class = self.get_form_class()
form = self.get_form(form_class)
pk = self.get_object().id
d = DashboardModel.objects.get(id=pk)
files = request.FILES.getlist('image')
if form.is_valid():
f = form.save(commit=False)
f.user = request.user
f.save()
if(d.image_set.all()): #if images are present in the record
for i in files:
Image.objects.update_or_create(project=f, image=i)
#d.image_set.all().delete() #using Django Cleanup, no need to do this
else:
for i in files:
Image.objects.create(project=f, image=i)
messages.success(request, "New images updated")
return self.form_valid(form)
else:
print(form.errors)
def get_success_url(self):
return reverse_lazy('record_detail', kwargs={'pk': self.object.pk})
but I’m still getting the same error now, I don’t know enough about HTTP to truly understand this error. It seemed like self.form_valid(form) was returning the response as I could successfully update records like this before overriding clean()
I meant I just changed the return (what’s highlighted). It’s guess work for me now, I’m close to giving up. I can only return a response from these View methods because it is the whole class which is the view.
def post(self, request, *args, **kwargs):
form_class = self.get_form_class()
form = self.get_form(form_class)
if form.is_valid():
f = form.save(commit=False)
f.user = request.user
f.save()
#return self.form_valid(form)
response = HttpResponse()
response.content = self.form_valid(form)
return response
I appreciate the helping hand, give me a while and I will try to make sure I understand what I’m doing again because I’m starting to become blind to it
I was initially trying to make conditions within form_valid and that wasn’t working properly, but I didn’t realize at that point that this method only reflects on the POSTed data.
I still don’t get this to work by overriding def post and returning response in the same way (calling the superclass constructor)
edit… it’s working in post now:
I just had the return in the wrong place, can be easy to get confused by the tabbing