|as_crispy_field got passed an invalid or inexistent field| :django

i have eror and i dont know what is mistake
html:

{% load static %}
{% load crispy_forms_tags %}
<!DOCTYPE html>
<html>
<head>
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@5.2.3/dist/css/bootstrap.min.css" integrity="sha384-rbsA2VBKQhggwzxH7pPCaAqO46MgnOM80zW1RWuH61DGLwZJEdK2Kadq2F9CUG65" crossorigin="anonymous">
    <link href="{% static 'css/stylesheet.css' %}" rel="stylesheet">
    <title>{{ title }}</title>
</head>
<body>
<div class="myForm">
    <form method='POST' action=''>{% csrf_token %}
        <div class="row">
            <div class='col-sm-6'>
                <div class="form-row">
                  <div class="form-group col-md-6">
                    {{ form.invoice_date|as_crispy_field }}
                    {{ form.name|as_crispy_field }}
                  </div>
                  <div class="form-group col-md-6">
                    {{ form.invoice_number|as_crispy_field }}
                    {{ form.phone_number|as_crispy_field }}
                  </div>
                </div>
                <div class="form-row">
                  <div class="form-group col-md-6">
                    {{ form.line_one|as_crispy_field }}
                  </div>
                  <div class="form-group col-md-2">
                    {{ form.line_one_quantity|as_crispy_field }}
                  </div>
                  <div class="form-group col-md-2">
                    {{ form.line_one_unit_price|as_crispy_field }}
                  </div>
                  <div class="form-group col-md-2">
                    {{ form.line_one_total_price|as_crispy_field }}
                  </div>
                </div>

                <div class="form-row">
                  <div class="form-group col-md-6">
                    {{ form.line_two|as_crispy_field }}
                  </div>
                  <div class="form-group col-md-2">
                    {{ form.line_two_quantity|as_crispy_field }}
                  </div>
                  <div class="form-group col-md-2">
                    {{ form.line_two_unit_price|as_crispy_field }}
                  </div>
                  <div class="form-group col-md-2">
                    {{ form.line_two_total_price|as_crispy_field }}
                  </div>
                </div>
                <div class="form-row">
                  <div class="form-group col-md-12">
                    {{ form.invoice_type|as_crispy_field }}
                  </div>
                  <div class="form-group col-md-12">
                    {{ form.total|as_crispy_field }}
                  </div>
                </div>

                <button type="submit" class="btn btn-primary">Save</button>

                <!-- <input type="submit" value='Submit'/>   -->
            </div>
        </div>
    </form>
</div><!-- End myform -->
</body>
</html>

models.py:

from django.db import models


class Invoice(models.Model):
    comments = models.TextField(max_length=3000, default='', blank=True, null=True)
    invoice_number = models.IntegerField(blank=True, null=True)
    invoice_date = models.DateField(auto_now_add=False, auto_now=False, blank=True, null=True)
    name = models.CharField('Customer Name', max_length=120, default='', blank=True, null=True)

    line_one = models.CharField('Line 1', max_length=120, default='', blank=True, null=True)
    line_one_quantity = models.IntegerField('Quantity', default=0, blank=True, null=True)
    line_one_unit_price = models.IntegerField('Unit Price (D)', default=0, blank=True, null=True)
    line_one_total_price = models.IntegerField('Line Total (D)', default=0, blank=True, null=True)

    line_two = models.CharField('Line 2', max_length=120, default='', blank=True, null=True)
    line_two_quantity = models.IntegerField('Quantity', default=0, blank=True, null=True)
    line_two_unit_price = models.IntegerField('Unit Price (D)', default=0, blank=True, null=True)
    line_two_total_price = models.IntegerField('Line Total (D)', default=0, blank=True, null=True)

    line_three = models.CharField('Line 3', max_length=120, default='', blank=True, null=True)
    line_three_quantity = models.IntegerField('Quantity', default=0, blank=True, null=True)
    line_three_unit_price = models.IntegerField('Unit Price (D)', default=0, blank=True, null=True)
    line_three_total_price = models.IntegerField('Line Total (D)', default=0, blank=True, null=True)

    line_four = models.CharField('Line 4', max_length=120, default='', blank=True, null=True)
    line_four_quantity = models.IntegerField('Quantity', default=0, blank=True, null=True)
    line_four_unit_price = models.IntegerField('Unit Price (D)', default=0, blank=True, null=True)
    line_four_total_price = models.IntegerField('Line Total (D)', default=0, blank=True, null=True)

    line_five = models.CharField('Line 5', max_length=120, default='', blank=True, null=True)
    line_five_quantity = models.IntegerField('Quantity', default=0, blank=True, null=True)
    line_five_unit_price = models.IntegerField('Unit Price (D)', default=0, blank=True, null=True)
    line_five_total_price = models.IntegerField('Line Total (D)', default=0, blank=True, null=True)

    phone_number = models.CharField(max_length=120, default='', blank=True, null=True)
    total = models.IntegerField(default='0', blank=True, null=True)
    balance = models.IntegerField(default='0', blank=True, null=True)
    timestamp = models.DateTimeField(auto_now_add=True, auto_now=False)
    last_updated = models.DateTimeField(auto_now_add=False, auto_now=True, blank=True)
    paid = models.BooleanField(default=False)
    invoice_type_choice = (
        ('Receipt', 'Receipt'),
        ('Proforma Invoice', 'Proforma Invoice'),
        ('Invoice', 'Invoice'),
    )
    invoice_type = models.CharField(max_length=50, default='', blank=True, null=True, choices=invoice_type_choice)
    def __str__(self):
        return self.name

    def __unicode__(self):
        return self.invoice_number

forms.py:

from django import forms
from .models import Invoice

class InvoiceForm(forms.ModelForm):
	class Meta:
		model = Invoice
		fields = ['name', 'phone_number', 'invoice_date',
				'line_one', 'line_one_quantity', 'line_one_unit_price', 'line_one_total_price',
				'total', 'paid', 'invoice_type'
				]

views.py:

from django.shortcuts import render , redirect
from .forms import InvoiceForm
from .models import *
def home(request):
	title = 'صورت حساب'
	context = {
	"title": title,
	}
	return render(request, "home.html",context)



def add_invoice(request):
	form = InvoiceForm(request.POST or None)
	if form.is_valid():
		form.save()
		return redirect('/add_invoice')
	context = {
		"form": form,
		"title": "New Invoice",
	}
	return render(request, "add_invoice.html", context)




def list_invoice(request):
	title = 'List of Invoices'
	queryset = Invoice.objects.all()
	context = {
		"title": title,
		"queryset": queryset,
	}
	return render(request, "list_invoice.html", context)

Welcome!

When posting code (or templates) here, enclose the code (or template) between lines of three backtick - ` characters. This means you’ll have a line of ```, then your code (or template), then another line of ```. This forces the forum software to keep your code properly formatted. (I’ve taken the liberty of editing your initial post to include them.)

Also, when requesting assistance with an error, please post the complete error message and the traceback as part of your post, also enclosed between lines of ```.

CrispyError at /add_invoice/
|as_crispy_field got passed an invalid or inexistent field
Request Method: GET
Request URL: http://127.0.0.1:8000/add_invoice/
Django Version: 4.2.3
Exception Type: CrispyError
Exception Value:
|as_crispy_field got passed an invalid or inexistent field
Exception Location: D:\Invice\venv\lib\site-packages\crispy_forms\templatetags\crispy_forms_filters.py, line 99, in as_crispy_field
Raised during: Invoice.views.add_invoice
Python Executable: D:\Invice\venv\Scripts\python.exe
Python Version: 3.10.1
Python Path:
[‘D:\Invice’,
‘C:\Users\user\AppData\Local\Programs\Python\Python310\python310.zip’,
‘C:\Users\user\AppData\Local\Programs\Python\Python310\DLLs’,
‘C:\Users\user\AppData\Local\Programs\Python\Python310\lib’,
‘C:\Users\user\AppData\Local\Programs\Python\Python310’,
‘D:\Invice\venv’,
‘D:\Invice\venv\lib\site-packages’]
Server time: Mon, 31 Jul 2023 12:01:32 +0000

This appears to be the error message that you would be seeing in your browser. We are more interested in the error message and traceback that is displayed in the console window where you’re running the server (probably either runserver or runserver_plus)

hi ken
this eror:

Error during template rendering
In template D:\Invice\templates\add_invoice.html, error at line 21

|as_crispy_field got passed an invalid or inexistent field
11	<div class="myForm">
12	    <form method='POST' action=''>{% csrf_token %}
13	        <div class="row">
14	            <div class='col-sm-6'>
15	                <div class="form-row">
16	                  <div class="form-group col-md-6">
17	                    {{ form.invoice_date|as_crispy_field }}
18	                    {{ form.name|as_crispy_field }}
19	                  </div>
20	                  <div class="form-group col-md-6">
21	                    {{ form.invoice_number|as_crispy_field }}
22	                    {{ form.phone_number|as_crispy_field }}
23	                  </div>
24	                </div>
25	                <div class="form-row">
26	                  <div class="form-group col-md-6">
27	                    {{ form.line_one|as_crispy_field }}
28	                  </div>
29	                  <div class="form-group col-md-2">
30	                    {{ form.line_one_quantity|as_crispy_field }}
31	                  </div>



and this track back:
## Traceback [Switch to copy-and-paste view](http://127.0.0.1:8000/add_invoice/#)

* `D:\Invice\venv\lib\site-packages\django\core\handlers\exception.py`, line 55, in inner

  55. response = get_response(request)

…

Local vars

|||
| --- | --- |
|||
|||
|||

* `D:\Invice\venv\lib\site-packages\django\core\handlers\base.py`, line 197, in _get_response

  197. response = wrapped_callback(request, *callback_args, **callback_kwargs)

…

Local vars

|||
| --- | --- |
|||
|||
|||
|||
|||
|||
|||
|||

* `D:\Invice\Invoice\views.py`, line 22, in add_invoice

  22. return render(request, "add_invoice.html", context)

…

Local vars

|||
| --- | --- |
|||
|||
|||

* `D:\Invice\venv\lib\site-packages\django\shortcuts.py`, line 24, in render

  24. content = loader.render_to_string(template_name, context, request, using=using)

…

Local vars

|||
| --- | --- |
|||
|||
|||
|||
|||
|||

* `D:\Invice\venv\lib\site-packages\django\template\loader.py`, line 62, in render_to_string

  62. return template.render(context, request)

…

Local vars

|||
| --- | --- |
|||
|||
|||
|||
|||

* `D:\Invice\venv\lib\site-packages\django\template\backends\django.py`, line 61, in render

  61. return self.template.render(context)

…

Local vars

|||
| --- | --- |
|||
|||
|||

* `D:\Invice\venv\lib\site-packages\django\template\base.py`, line 175, in render

  175. return self._render(context)

…

Local vars

|||
| --- | --- |
|||
|||

* `D:\Invice\venv\lib\site-packages\django\template\base.py`, line 167, in _render

  167. return self.nodelist.render(context)

…

Local vars

|||
| --- | --- |
|||
|||

* `D:\Invice\venv\lib\site-packages\django\template\base.py`, line 1005, in render

  1005. return SafeString("".join([node.render_annotated(context) for node in self]))

…

Local vars

|||
| --- | --- |
|||
|||

* `D:\Invice\venv\lib\site-packages\django\template\base.py`, line 1005, in <listcomp>

  1005. return SafeString("".join([node.render_annotated(context) for node in self]))

…

Local vars

|||
| --- | --- |
|||
|||
|||

* `D:\Invice\venv\lib\site-packages\django\template\base.py`, line 966, in render_annotated

  966. return self.render(context)

…

Local vars

|||
| --- | --- |
|||
|||

* `D:\Invice\venv\lib\site-packages\django\template\base.py`, line 1064, in render

  1064. output = self.filter_expression.resolve(context)

…

Local vars

|||
| --- | --- |
|||
|||

* `D:\Invice\venv\lib\site-packages\django\template\base.py`, line 742, in resolve

  742. new_obj = func(obj, *arg_vals)

…

Local vars

|||
| --- | --- |
|||
|||
|||
|||
|||
|||
|||
|||

* `D:\Invice\venv\lib\site-packages\crispy_forms\templatetags\crispy_forms_filters.py`, line 99, in as_crispy_field

  99. raise CrispyError("|as_crispy_field got passed an invalid or inexistent field")

So, in your error message at the top, it’s telling you which line of the template is causing the error.

At what line is the error occurring, and what is that line in your template?

my eror in line 21
{{ form.invoice_number|as_crispy_field }}

So that error is telling you that invoice_number is not a field in your form.

now i have new problem the crispy form by bootstrap not work

it is not true

What is the issue? Please describe what you think is wrong in as much detail as you can provide.