1

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.

Elsa
  • 1
  • 1
  • 8
  • 27

3 Answers3

2

You don't do this in form_valid - as the name implies, by that point the form is already considered to be valid. In fact you don't do it in the view at all. This sort of thing belongs in the form, specifically in the clean() method of ProjectForm. There you can raise forms.ValidationError with your message; the view will then do the right thing and redisplay the invalid form.

Daniel Roseman
  • 588,541
  • 66
  • 880
  • 895
  • Thank you so much Daniel, but it seems like forms.ValidationError is read for programmer not for clients, it's too ugly – Elsa Aug 16 '18 at 09:13
  • No, that's not true at all. – Daniel Roseman Aug 16 '18 at 09:46
  • Hi Daniel, no matter how I changed the code, I always got a ugly error which looks like a bug of program, I really need advice from you, thank you so much for your great help! – Elsa Aug 17 '18 at 02:28
  • Hi Daniel, Really need your advice, I can't add forms.ValidationError in forms.py and also can't add form.add_error in views.py, as when I add test = Test.objects.get(pk=self.kwargs['pk']) in forms.py I get error of 'ProjectForm' object has no attribute 'kwargs' – Elsa Aug 21 '18 at 01:13
  • If you're using self.kwargs in the form to bed to define it in the init method. – Daniel Roseman Aug 21 '18 at 07:34
2

The general approach to make a form submit invalid, is to render the given form with errors.

You can either send some necessary data to the form and customise the form's clean method...

import forms

class Projectform(forms.ModelForm):
    def __init__(user_project, request, *args, **kwargs):
        self.user_project = user_project
        self.request = request
        super().__init__(*args, **kwargs)

    def clean(self, *args, **kwargs)
        for u in self.user_project:
            user_times = int(sum(t['learn_times'] for t in times))
            if user_times >= 8 or int(self.request.POST.get('learn_times')) + user_times >= 8:
            raise forms.ValidationError(u.username + "'s learn_times is more than 8 hours, please check!")
        return super().clean(*args, **kwargs)

... or you can customize the form_valid method of the view and re-render the form with the new error message.

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:
                form.add_error('__all__', self.request, u.username + "'s learn_times is more than 8 hours, please check!")
                return super().form_invalid(form)

    messages.success(self.request, 'Project created successfully!')
    return super().form_valid(form)

The errors will appear {{ form.non_field_errors }} in the template.

Tobias Lorenz
  • 1,426
  • 11
  • 17
  • Hi Tobias, I get the error of `'ProjectForm' object has no attribute 'kwargs'`, as I have to get a object from another model by `test = Test.objects.get(pk=self.kwargs['pk']), user_project = list(UserProject.objects.filter(test=test))` – Elsa Aug 17 '18 at 00:58
  • Hi Tobias, If I use `request.POST.get()` on Projectform of forms.py, also will get the error of `'ProjectForm' object has no attribute request`....... – Elsa Aug 17 '18 at 02:42
  • Hi @Tobias Lorenz Really need your advice, I can't add `forms.ValidationError` in forms.py and also can't add `form.add_error` in views.py, as when I add `test = Test.objects.get(pk=self.kwargs['pk'])` in forms.py I get error of `'ProjectForm' object has no attribute 'kwargs'` – Elsa Aug 21 '18 at 01:12
  • @Begin2Pip `self.kwargs` is set in the init function of the form. You need to add your code after calling `super().__init__(*args, **kwargs)`. – Tobias Lorenz Aug 21 '18 at 05:33
0

While using the answer above by Tobias and pass pk as a variable in method

test=Test.objects.get(pk=self.pk)