Reputation: 25
im getting this error as_crispy_field got passed an invalid or inexistent field
every time im trying to use as_crispy_field with forms
here is my code
models.py
class Customer_Email(models.Model):
subject=models.CharField(max_length=50,null=True,blank=True)
message=models.TextField(max_length=1000,null=True,blank=True)
file_upload=models.FileField(null=True,blank=True)
sender=models.ForeignKey(User,on_delete=models.CASCADE ,null=True,blank=True)
company=models.ForeignKey(Customer,on_delete=models.CASCADE ,null=True,blank=True)
date=models.DateTimeField(auto_now=True)
views.py
def send_email(request,id):
customer=get_object_or_404(Customer,pk=id)
form=Customer_Email_Form(request.POST)
customers=Customer.objects.all()
context={"customer":customer,"email_form":form,"customers":customers}
if request.method == 'GET':
return render(request,'crm/email_form.html',context)
if request.method=='POST':
if form.is_valid():
form.save()
messages.success(request,"Email Sent")
return render(request,'crm/listing.html',context)
return render(request,'crm/email_form.html',context)
html
{% load crispy_forms_tags %}
<form class="" method="POST">
{% csrf_token %}
<div class="form-group m-2">
<label>Subject</label>
{{email_form.subject|as_crispy_field}}
</div>
<div class="form-group m-2">
<label>Message</label>
{{email_form.message|as_crispy_field}}
</div>
<div class="form-group m-2">
<label>Uplaod</label>
{{email_form.file_upload|as_crispy_field}}
<span class="color-secondary">you can attach 2M files (pdf,doc)</span>
</div>
{{email_form.company|as_crispy_field}}
{{email_form.sender|as_crispy_field}}
<button
class="btn btn-primary btn-lg mt-5"
type="submit"
hx-post="email_form/p={{customer.id}}"
hx-target="#crm-list"
data-dismiss="modal"
>Send Email <i class='bx bx-mail-send bx-xl'></i></button>
</form>
forms.py
class Customer_Email_Form(forms.ModelForm):
class Meta:
model=Customer_Email
fields=['subject','file_upload','message','sender','company']
i have tried to change it to forms.Form but it gives me the same error i dont know what excactly i should do and im new to it
Upvotes: 0
Views: 470
Reputation: 81
Try this...
def send_email(request,id):
customer=get_object_or_404(Customer,pk=id)
form=Customer_Email_Form()
customers=Customer.objects.all()
context={"customer":customer,"email_form":form,"customers":customers}
if request.method == 'GET':
return render(request,'crm/listing.htm.html',context)
if request.method=='POST':
form=Customer_Email_Form(request.POST)
if form.is_valid():
form.save()
messages.success(request,"Email Sent")
return render(request,'crm/listing.html',context)
return render(request,'crm/listing.html',context)
You were passing POST data to your form before any was sent, and then rendering the form. I find it easier/clearer to restructure as such:
def send_email(request,id):
if request.method=='POST':
form=Customer_Email_Form(request.POST)
if form.is_valid():
form.save()
messages.success(request,"Email Sent")
return redirect('send_email', id)
else:
messages.error(request, form.errors)
return redirect('send_email', id)
else:
customer=get_object_or_404(Customer,pk=id)
form=Customer_Email_Form()
customers=Customer.objects.all()
context={"customer":customer,"email_form":form,"customers":customers}
return render(request,'crm/listing.html',context)
Assuming that you have imported redirect
and your url name in urls.py
is 'send_email'
.
Upvotes: 0