Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Disabling a crispy form field from showing errors

I have a form to perform searching. As I am using the primary key to search, The searching process is completed successfully BUT I used to get an error below the text field saying Invoice number already exists. I did some tweaks and stopped the form from showing errors but the text field still has a red outline whenever I perform the searching operation. How can I stop the form from doing that?

The code in the forms.py that disabled the form to show field errors:

class InvoiceSearchForm(forms.ModelForm):
  generate_invoice = forms.BooleanField(required=False)
class Meta:
    model = Invoice
    fields = ['invoice_number', 'name','generate_invoice']

def __init__(self, *args, **kwargs):
    super(InvoiceSearchForm, self).__init__(*args, **kwargs)
    self.helper = FormHelper(self)
    self.helper.form_show_errors = False
    self.helper.error_text_inline = False
    self.form_error_title=False

The HTML code that deals with the search operation:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

<div class="myForm">
        <form method='POST' action=''>{% csrf_token %}
            <div class="row">
                <div class='col-sm-12'>
                    <div class="form-row">
                        <div class="form-group col-md-3">
                            {{ form.invoice_number|as_crispy_field }}
                        </div>
                        <div class="form-group col-md-3">
                            {{ form.name|as_crispy_field }}
                        </div>
                        <div class="form-group col-md-3">
                            {{ form.generate_invoice|as_crispy_field }}
                        </div>
                        <div class="form-group col-md-3">
                            <br>
                            <button type="submit" class="btn btn-primary">Search</button>
                        </div>
                    </div>
                </div>
            </div>
        </form>
    </div>

The views.py related to the search operation:

@login_required
def list_invoice(request):
  title = 'List of Invoices'
  queryset = Invoice.objects.all()
  form = InvoiceSearchForm(request.POST or None)
  context = {
    "title": title,
    "queryset": queryset,
    "form":form,
  }

  if request.method == 'POST':
       queryset = Invoice.objects.filter(invoice_number__icontains=form['invoice_number'].value(),name__icontains=form['name'].value())
context = {
    "form": form,
    "title": title,
    "queryset": queryset,
}
return render(request, "list_invoice.html", context)

The red outline of the textbox that I get after performing search operation-> Screenshot

>Solution :

I think you need to use forms.Form instead of forms.ModelForm which is designed for creating and updating Model instances.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading