Home > Blockchain >  Disabling a crispy form field from showing errors
Disabling a crispy form field from showing errors

Time:07-10

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:

<div >
        <form method='POST' action=''>{% csrf_token %}
            <div >
                <div class='col-sm-12'>
                    <div >
                        <div >
                            {{ form.invoice_number|as_crispy_field }}
                        </div>
                        <div >
                            {{ form.name|as_crispy_field }}
                        </div>
                        <div >
                            {{ form.generate_invoice|as_crispy_field }}
                        </div>
                        <div >
                            <br>
                            <button type="submit" >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

CodePudding user response:

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

  • Related