Sashaank
Sashaank

Reputation: 964

crispy_forms.exceptions.CrispyError: |as_crispy_field got passed an invalid or inexistent field

I am working on a django application. In the application there exists a contribution form. I created the form with django models and django forms. But when I try to run the application, I get the following error.

crispy_forms.exceptions.CrispyError: |as_crispy_field got passed an invalid or inexistent field

I am not sure what I am doing wrong.

django models.py


CONTRIBUTE_CHOICE = (
    ('Books', 'Books'),
    ('Renovation', 'Renovation'),
    ('Other', 'Other'),
    )

class Contribute(models.Model):
    firstName = models.CharField(max_length=50)
    lastName = models.CharField(max_length=50)
    email = models.EmailField(max_length=100)
    contribution = models.CharField(max_length=100, choices=CONTRIBUTE_CHOICE)

    def publish(self):
        self.save()

    def __str__(self):
        return self.firstName

django forms.py

from .models import Contribute

CONTRIBUTE_CHOICE = (
    ('Books', 'Books'),
    ('Renovation', 'Renovation'),
    ('Other', 'Other'),
    )

class ContributeForm(forms.ModelForm):

    contribution = forms.ChoiceField(choices=CONTRIBUTE_CHOICE, required=True )

    class Meta:
        model = Contribute

        widgets = {
            'firstName': forms.TextInput(attrs={'placeholder': 'First Name'}),
            'lastName': forms.TextInput(attrs={'placeholder': 'Last Name'}),
            'email': forms.TextInput(attrs={'placeholder': 'Email'}),
        }
        fields = ('firstName', 'lastName', 'email', 'contribution')

django views.py

from .forms import ContributeForm

def donate(request):
    if request.method == "POST":
        contributeForm = ContributeForm(request.POST)

        if contributeForm.is_valid():
            post = contributeForm.save(commit=False)
            post.save()
            return redirect('home')

    else:
        contributeForm = ContributeForm()
        context = {'contributeForm': contributeForm}
        return render(request, 'donate.html', context)

donate.html tempate

<form class='contribution_form' method="post">
    {% csrf_token %}
    <div class="row">
        <div class="col">
            {{ contributeForm.firstName|as_crispy_field }}
        </div>
        <div class="col">
            {{ contributeForm.lastName|as_crispy_field }}
        </div>
    </div>
    {{ contributeForm.email|as_crispy_field }}
    {{ contributeForm.contribution|as_crispy_field }}
    <button type="submit" class="btn btn-lg">Submit</button>
</form>

This is the link I followed to create the form

[EDIT-1]

I changed the code as per the answer and now I get the following error. Please help me

django.template.exceptions.TemplateSyntaxError: Invalid filter: 'as_crispy_field'

Upvotes: 0

Views: 12491

Answers (4)

Marcel M&#252;ller
Marcel M&#252;ller

Reputation: 1

Have you tried to add the field in the update view? Like the following

fields = [
    'field_1',
    'field_2',
]

If you have forgotten to add field_3 it will raise an error.

Upvotes: 0

Vicente Vasquez
Vicente Vasquez

Reputation: 11

Your form is name is:

ContributeForm

and in the template you writed as:

contributeForm

is keysensitive that's the problem

Upvotes: 1

ChristianH
ChristianH

Reputation: 63

I had the same error message. It was a variable / form field of type ForeignKey, which should present choices, that caused the problem.

Upvotes: 0

Linh Nguyen
Linh Nguyen

Reputation: 3880

you didn't pass your form to the donate.html tempate:

def donate(request):
  if request.method == "POST":
       contributeForm = ContributeForm(request.POST)

       if contributeForm.is_valid():
          post = contributeForm.save(commit=False)
          post.save()
          return redirect('home')
       else:
       # this should be include if form validate failed
          return render(request, 'donate.html', {'contributeForm': contributeForm})

  elif request.method == "GET":
       contributeForm = ContributeForm()
       context = {'contributeForm': contributeForm}
       # return render(request, 'index.html', context) <-- why do you have this here?
       return render(request, 'donate.html', context)

Upvotes: 2

Related Questions