Issue
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 medjango.template.exceptions.TemplateSyntaxError: Invalid filter: 'as_crispy_field'
Solution
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)
Answered By - Linh Nguyen
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.