How to render invalid forms with it's all previous value?

I want to prevent rendering empty forms if my froms is invalid. I want it will render with previous data those typed in each fields. Right now it’s rendering empty forms if my forms is invalid.
here is my code:


<form method="POST">
{% csrf_token %}
<button class="btn btn-primary" type="submit">Submit</button>

class CommentFrom(forms.ModelForm):

      captcha = CaptchaField()

      class Meta:

          model = BlogComment

          fields = ['name','email','comment',]

          widgets = {

            'name': forms.TextInput(attrs={'class':'form-control','placeholder': 'name'}),

            'email': forms.TextInput(attrs={'class':'form-control','placeholder': 'email'}),

            'comment': forms.Textarea(attrs={'class':'form-control','placeholder': 'comment'}),



if form.is_valid():

             comment = 

    = blog


             messages.add_message(self.request, messages.INFO, 'Your Comment pending for admin approval')

             return redirect(reverse('blog-detail', kwargs={'slug':slug}))



               form = CommentFrom()

               messages.add_message(self.request, messages.INFO, 'Captcah verification failed')

               return redirect(reverse('blog-detail', kwargs={'slug':slug}))

See the docs for Dynamic initial values for forms. You can initialize the form with the data previously submitted for the form.

Also, as a side note, you might want to work on spelling “forms” correctly. (It’s not “froms”, it’s “forms”.) Part of the value of this forum is the ability for others to search for problems that they’re having, and having the component names spelled correctly helps those other people.

1 Like

KenWhitesell Thanks and I correct the spelling.