I would like to know how to display Django message.error before form.save(if user get that error message, the form won't be saved, user have to fill value in form again).
I don't know whether I put them at wrong place or anything else reason, I can definitely get the right value about for loop and if else
, but if a user's work_times >= 8 hours, page didn't display that error message, and project can save like before, but I did add for loop and if else
!! The partial code of views.py is like this:
class ProjectCreateView(CreateView):
model = Project
form_class = ProjectForm
def form_valid(self, form):
request = self.request
for u in user_project:
user_times = int(sum(t['learn_times'] for t in times))
if user_times >= 8 or int(request.POST.get('learn_times')) + user_times >= 8:
messages.error(self.request, u.username + "'s learn_times is more than 8 hours, please check!")
else:
pass
project = form.save(commit=False)
project.save()
form.save_m2m()
messages.success(self.request, 'Project created successfully!')
return super(CoursePermitCreateView, self).form_valid(form)
def get_success_url(self):
return reverse('project_change', kwargs={'pk': self.object.pk})
Thanks so much for any advice.