IntegrityError at /send/ NOT NULL constraint failed: user_transactions.user_id - django

enter code heremodels
class Transactions(models.Model):
user=models.ForeignKey(CustomUser,related_name='accounts',on_delete=models.CASCADE, blank=True) recipient_account_type = models.CharField(max_length=230) recipient_name = models.CharField(max_length=100) recipient_acount_number = models.PositiveIntegerField() recipient_routing_number = models.PositiveIntegerField() recipient_bank_name = models.CharField(max_length=200) recipient_swift_code = models.CharField(max_length=100) recipient_sending_amount = models.DecimalField(max_digits=15, decimal_places=2) transaction_date = models.DateTimeField(auto_now_add=True) transaction_ID =models.IntegerField(default=ID) description = models.CharField(max_length=1000)
forms
class TransactionForm(forms.ModelForm):
class Meta:
model = Transactions
fields = ['recipient_account_type','recipient_name','recipient_acount_number',
'recipient_routing_number','recipient_bank_name','recipient_swift_code','recipient_sending_amount','description',]
Widget = forms.TextInput(attrs={'autofocus':'autofocus','size':'40', 'font-size':'xx-large'})
views
#login_required(login_url='/login/')
def send(request):
posts =Transactions.objects.filter(user=request.user)
form = TransactionForm()
if request.method=="POST":
form = TransactionForm(request.POST, request.FILES)
if form.is_valid():
form.save()
return redirect('/success/')
else:
print(form.errors)
# messages.error(request, 'Failed, Resend')
context = {'form':form,'posts':posts}
return render(request, 'index_user/send-money.html', context)
**template**
{% extends 'about/user-base.html' %}
{% load crispy_forms_tags %}
{% load static %}
{% load humanize %}
{% block content %}
<!-- Content
============================================= -->
<div id="content" class="py-4">
<div class="container">
<div class="row mt-4 mb-5">
<div class="col-lg-11 mx-auto">
<div class="row widget-steps">
<div class="col-12 step active">
<div class="step-name">Details</div>
</div>
</div>
</div>
</div>
<h2 class="fw-400 text-center mt-3">Send Money</h2>
<p class="lead text-center mb-4">Send your money at anytime, anywhere in the world.</p>
<div class="row">
<div class="col-md-18 col-lg-7 col-xl-6 mx-auto bg-primary">
<div class="bg-white shadow-sm rounded p-3 pt-sm-4 pb-sm-5 px-sm-5 mb-4">
<hr class="mx-n3 mx-sm-n5 mb-4">
<div class="bg-white shadow-sm rounded p- pt-sm-5 pb-sm-5 px-sm-5 mb-4 bg-secondary">
<div class="text-center bg-primary p-4 rounded mb-4">
<h3 class="text-10 text-white fw-400">${{user.account_balance|intcomma}}</h3>
<p class="text-white">Available Balance</p>
{% comment %} Withdraw Full Amount </div> {% endcomment %}
<!-- Send Money Form
============================ -->
<p style="color: red; font-size:large">
{% if messages %}
{% for message in messages %}
{{message}}
{% endfor %}
{% endif %}
</p>
<center style="background-color: #007bff;">
<form id="form-send-money" method="POST" action="" class="bg-success" >
{% csrf_token %}
<div class="mb-3 pb-sm-5 px-sm-5">
{% comment %} <label for="withdrawto" class="form-label">Withdraw From</label> {% endcomment %}
<br>
{% comment %} <select id="withdrawto" class="form-select px-sm-4 pb-sm-1" name="">
<option value="">FNMB Bank - {{user.account_number}}</option>
</select> {% endcomment %}
</div>
<div id="id_long_desk" class="mb-3 px-sm-4 pb-sm-9">
<label for="emailID" class="form-label">Recipient Full Name</label>
<br>
{{form.recipient_name}}
</div>
<div class="mb-3 px-sm-4 pb-sm-1">
<label for="emailID" class="form-label">Recipient Account Number</label>
<br>
{{form.recipient_acount_number}}
</div>
<div class="mb-3 px-sm-4 pb-sm-1">
<label for="emailID" class="form-label">Recipient Bank Name</label>
<br>
{{form.recipient_bank_name}}
</div>
<div class="mb-3 px-sm-4 pb-sm-1">
<label for="emailID" class="form-label">Recipient Routing Number</label>
<br>
{{form.recipient_routing_number}}
</div>
<div class="mb-3 px-sm-4 pb-sm-1">
<label for="emailID" class="form-label">Recipient Swift Code</label>
<br>
{{form.recipient_swift_code}}
</div>
<div class="mb-3 px-sm-4 pb-sm-1">
<label for="emailID" class="form-label">Recipient Account Type</label>
<br>
{{form.recipient_account_type}}
</div>
<div class="mb-3 px-sm-4 pb-sm-1">
<label for="emailID" class="form-label">Recipient Sending Amount</label>
<br>
{{form.recipient_sending_amount}}
</div>
<div class="mb-3 px-sm-4 pb-sm-1">
<label for="emailID" class="form-label">Description</label>
<br>
{{form.description}}
</div>
{% comment %} <div class="mb-3 px-sm-4 pb-sm-1">
<label for="emailID" class="form-label">Fees</label>
<br>
$ {{form.fees}}
</div> {% endcomment %}
<!-- <p>Transfer Fees <span class="float-end"> 12.21 USD</span></p> -->
<hr>
<div class="d-grid"><button class="submit btn btn bg-white">Send</button></div>
<hr>
</form>
</center>
<!-- Send Money Form end -->
</div>
</div>
</div>
</div>
</div>
<!-- Content end -->
{% endblock %}

Have you checked whether your form is valid with something like the following?
if form.is_valid():
obj = form.save()
else:
# errror handling? debug with form.errors...
If your code does look like above, please add more info (like the form handling method as a whole)

Related

Problems with a backend part of search line in Django

who can explain me why my SearchView doesn't work. I have some code like this.It doesn't show me any mistakes, but it doesn't work. The page is clear. Seems like it doesn't see the input.
search.html
<div class="justify-content-center mb-3">
<div class="row">
<div class="col-md-8 offset-2">
<form action="{% url 'search' %}" method="get">
<div class="input-group">
<input type="text" name="q" class="form-control" placeholder="Search..." />
<div class="input-group-append">
<button class="btn btn-dark" type="submit" id="button-addon2">Search</button>
</div>
</div>
</form>
</div>
</div>
</div>
search/urls.py
path('search/', SearchView.as_view(), name='search')
search/views.py
class SearchView(ListView):
model = Question
template_name = 'forum/question_list.html'
def get_queryset(self):
query = self.request.GET.get("q")
object_list = Question.objects.filter(
Q(title__icontains=query) | Q(detail__icontains=query)
)
return object_list
forum/question_list.html
{% extends 'main/base.html' %}
{% block content %}
{% for question in object_list %}
<div class="card mb-3">
<div class="card-body">
<h4 class="card-title">{{ question.title }}</h4>
<p class="card-text">{{ question.detail }}</p>
<p>
{{ question.user.username }}
5 answers
10 comments
</p>
</div>
</div>
{% endfor %}
{% endblock %}

Django - I want to display current rate depending on the category i select

here is my front-end and everything is working fine however, i don't want all rate to display at once, i want the rate to display one at a time depending on the card category i select.
From the image i uploaded, current rate of other card category are showing which i don't want, please i need help even if it will require using javascript of ajax
Here is my index(template)
{% for giftcard in giftcards %}
<!-- Card -->
<div class="col-lg-4 gift__card col-6 p-0">
<a type="button" class="btn">
<img class="img-fluid gift__card-img" src="{{ giftcard.card_image.url }}">
</a>
<div class="container d-flex align-items-center justify-content-center">
<div class="gift__card-modal-container py-5">
<div class="card__container">
<div class="gift__card-overlay"></div>
<div class="container-fluid bg-light gift__card-modal shadow-lg">
<div class="pop p-5">
<div class="row d-flex align-items-center justify-content-between">
<div class="col-lg-5 col-12 p-0 m-0">
<img class="img-fluid gift__card-img" style="width: 40rem;" src="{{ giftcard.card_image.url }}">
<p class="text-muted">Select the card category and the amount.</p>
</div>
<div class="col-lg-6 col-sm-12 card-details">
<form class="card-form">
<div class="form-group py-2">
<label for="card-category">Card category</label>
<select id="category" class="form-select py-2" aria-label="Default select example">
{% for spec in giftcard.category_set.all %}
<option value="{{ spec.category }}">{{ spec.category }}</option>
{% endfor %}
</select>
</div>
<div class="form-group py-2">
<label for="Amount">Amount</label>
<div class="d-flex align-items-center amount">
<input type="text" class="form-control" id="amount"
placeholder="Please enter amount">
<span class="">#100,000</span>
</div>
</div>
<div class="form-group py-3">
{% for spec in giftcard.category_set.all %}
<label for="rate">Current rate - {{ spec.rate }}</label>
{% endfor %}
</div>
<div class="border-none pt-2 pl-3 d-flex justify-content-end">
<button type="button" class="btn process-card-btn">Proceed</button>
</div>
</form>
</div>
</div>
</div>
</div>
<div class="gift__terms-card hide fade" id="terms" tabindex="-1" role="dialog"
aria-labelledby="exampleModalCenterTitle" aria-hidden="true">
<div class="modal-dialog modal-dialog-centered" role="document">
<div class="modal-content terms">
<div class="p-5">
<h4 class="terms-title pb-4">Trading terms</h4>
{% for spec in giftcard.category_set.all %}
<p class="pb-2">{{ spec.terms }}</p>
{% endfor %}
<div class="border-none pt-3 pl-3 d-flex justify-content-end">
<button type="button" class="btn process-card-btn">Proceed</button>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
{% endfor %}
Here is my models
class Giftcard(models.Model):
name = models.CharField(max_length=100, unique=True)
card_image = models.ImageField(upload_to='Giftcard/', blank=False)
date = models.DateTimeField(auto_now_add=True)
publish = models.BooleanField(default=False)
class Category(models.Model):
category = models.CharField(max_length=250)
rate = models.IntegerField()
terms = models.TextField()
card_category = models.ForeignKey(Giftcard, on_delete=models.CASCADE)
Here is my views
def giftcard(request):
giftcards = Giftcard.objects.filter(publish=True)
context = {
'giftcards': giftcards
}
return render(request, 'dashboard/giftcard.html', context)

Django Form is not processing properly and no error has been displayed

Trying to create an app that saves sales in different sateges for my team(similar to an ecommerce)
however the form to add the account info is not saving the data...
I review different options and use formset as well no changes.
model.py
TYPE=(
('delegated','Delegated'),
('not_delegated','Not Delegated')
)
class Account(models.Model):
agent_profile=models.ForeignKey(AgentProfile, on_delete= CASCADE)
account_name=models.CharField(max_length=120)
opp_type=models.CharField(max_length=20, choices=TYPE, default='not_delegated')
bp_id=models.IntegerField()
mdm_id=models.CharField(max_length=50, null=True,blank=True)
AM=models.CharField(max_length=50,null=True,blank=True)
def __str__(self):
return str(self.agent_profile)
forms.py
from django.forms import ModelForm
from .models import Account
class AccountForm(ModelForm):
class Meta:
model=Account
fields=[
#'agent_profile',
'account_name',
'opp_type',
'bp_id',
'mdm_id',
'AM'
]
views.py
def confirm_account_create_view(request):
form=AccountForm(request.POST or None)
context = {
"form": form
}
next_ = request.GET.get('next')
next_post = request.POST.get('next')
redirect_path = next_ or next_post or None
if form.is_valid():
instance=form.save(commit=False)
agent_profile, agent_profile_created = AgentProfile.objects.new_or_get(request)
if agent_profile is not None:
opp_type = request.POST.get('delegate_opp', 'not_delegated_opp')
instance.agent_profile = agent_profile
instance.opp_type = opp_type
instance.save()
request.session[opp_type + "_opp_id"] = instance.id
print(opp_type + "_opp_id")
else:
print("Please verify Form")
return redirect("opps:confirm")
if is_safe_url(redirect_path, request.get_host()):
return redirect(redirect_path)
return redirect("opps:confirm")
urls.py (main app)
path('orders/confirm/account', confirm_account_create_view, name='create_account'),
form page(HTML)
{% csrf_token %}
{% if next_url %}
{% endif %}
{% if opp_type %}
<input type='hidden' name='opp_type' value='{{ opp_type }}' />
{% endif %}
{{ form.as_p }}
<button type='submit' class='btn btn-default'>Submit</button>
</form>
confirm page(HTML)
{% extends "base.html" %}
{% load static %}
{% block content %}
{{ object.order_id }} --{{object.opp}}
<div class="pb-9 mb-7 text-center border-b border-black border-opacity-5">
<h2 class="mb-7 lg:mt-6 text-3xl font-heading font-medium">Confirm Info</h2>
</div>
<h2 class="mb-7 lg:mt-6 text-3xl font-heading font-medium">Account</h2>
<div class='row'>
<div class="flex items-center justify-between py-4 px-10 mb-3 leading-8 bg-white bg-opacity-50 font-heading font-medium rounded-3xl">
{% if not object.delegated_opp %}
{% url "create_account" as confirm_account_create_view%}
{% include 'accounts/form.html' with form=delegated_form ext_url=request.build_absolute_uri action_url=confirm_account_create_view opp_type='not_delegated_opp' %}
</div>
<div class="flex items-center justify-between py-4 px-10 mb-3 leading-8 bg-white bg-opacity-50 font-heading font-medium rounded-3xl">
{% elif not object.not_delegated_opp %}
{% url "create_account" as confirm_account_create_view%}
{% include 'accounts/form.html' with form=delegated_form ext_url=request.build_absolute_uri action_url=confirm_account_create_view opp_type='not_delegated_opp' %}
</div>
</div>
{%else%}
<div>
<h2 class="mb-7 lg:mt-6 text-3xl font-heading font-medium">Order summary</h2>
<div class="flex items-center justify-between py-4 px-10 mb-3 leading-8 bg-white bg-opacity-50 font-heading font-medium rounded-3xl">
<span>Subtotal</span>
<span class="flex items-center text-xl">
<span class="mr-2 text-base">$</span>
<span>{{object.opp.subtotal}}</span>
</span>
</div>
<div class="flex items-center justify-between py-4 px-10 mb-3 leading-8 bg-white bg-opacity-50 font-heading font-medium rounded-3xl">
<span>Discount</span>
<span class="flex items-center text-xl">
<span class="mr-2 text-base">$</span>
<span>{{object.discount}}</span>
</span>
</div>
<div class="flex items-center justify-between py-4 px-10 mb-14 leading-8 bg-white font-heading font-medium rounded-3xl">
<span>Total</span>
<span class="flex items-center text-xl text-blue-500">
<span class="mr-2 text-base">$</span>
<span>{{object.total}}</span>
</span>
</div>
<a class="inline-block w-full py-5 lg:py-3 px-10 text-lg leading-6 lg:leading-7 text-white font-medium tracking-tighter font-heading text-center bg-blue-500 hover:bg-blue-600 focus:ring-2 focus:ring-blue-500 focus:ring-opacity-50 rounded-xl" href="#">Save Opp</a>
</div>
</div>
</div>
<div class="md:w-96"><a class="block py-5 px-10 w-full text-xl leading-6 font-medium tracking-tighter font-heading text-center bg-white hover:bg-gray-50 focus:ring-2 focus:ring-blue-500 focus:ring-opacity-50 rounded-xl" href="#">Back to top</a></div>
</div>
</section>
{% endif %}
{% endblock content %}
I type AM by mistake instead of am at a model level and form level.
Once I changed it to :
am=models.CharField(max_length=50,null=True,blank=True)
It began to work properly

My for loop does not work as expected - Data does not show up in my django template

I am trying to use a for loop in my Django template to show the data stored in the models of a table but for some reason , the data does not show up in the template.
Views.py
def add_part(request):
parts = Parts.objects.all()
context = {
"parts": parts
}
return render(request, 'admintemplate/add_parts_template.html', context)
def add_part_save(request):
if request.method != "POST":
messages.error(request, "Method Not Allowed!")
return redirect('add_part')
else:
part_name = request.POST.get('part_name')
part_type = request.POST.get('part_type')
supplier_id = request.POST.get('suppliers')
suppliers = Suppliers.objects.get(id=supplier_id)
try:
part = Parts(part_name=part_name, part_type=part_type, supplier_id=supplier)
part.save()
messages.success(request, "Part Added Successfully!")
return redirect('add_part')
except:
messages.error(request, "Failed to Add Part!")
return redirect('add_part')
models.py
The parts and the services model are exactly the same with different column names, so I think the functionality for both should be the same.
Suppliers models
class Suppliers(models.Model):
id = models.AutoField(primary_key=True)
name = models.CharField(max_length=20)
Parts model
class Parts(models.Model):
id = models.AutoField(primary_key=True)
name = models.CharField(max_length=20)
part_type = models.CharField(max_length=20)
supplier_id = models.ForeignKey(Suppliers, on_delete=models.CASCADE)
Services model
class Services(models.Model):
id = models.AutoField(primary_key=True)
name = models.CharField(max_length=20)
service_type = models.CharField(max_length=20)
supplier_id = models.ForeignKey(Suppliers, on_delete=models.CASCADE)
Part template
{% extends 'admintemplate/base_template.html' %}
{% block page_title %}
Add Parts
{% endblock page_title %}
{% block main_content %}
{% load static %}
<section class="content">
<div class="container-fluid">
<div class="row">
<div class="col-md-12">
<!-- general form elements -->
<div class="card card-primary">
<div class="card-header">
<h3 class="card-title">Add Parts</h3>
</div>
<!-- /.card-header -->
<!-- form start -->
<form role="form" method="POST" action="{% url 'add_part_save' %}">
{% csrf_token %}
{% comment %} Display Messages {% endcomment %}
{% if messages %}
<div class="form-group">
<div class="col-12">
{% for message in messages %}
{% if message.tags == "error" %}
<div class="alert alert-danger alert-dismissible fade show" role="alert" style="margin-top: 10px;">
{{ message }}
<button type="button" class="close" data-dismiss="alert" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
{% elif message.tags == "success" %}
<div class="alert alert-success alert-dismissible fade show" role="alert" style="margin-top: 10px;">
{{ message }}
<button type="button" class="close" data-dismiss="alert" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
{% endif %}
{% endfor %}
</div>
</div>
{% endif %}
<div class="card-body">
<div class="form-group">
<label>Part Name </label>
<input type="text" class="form-control" name="part_name" placeholder="Part Name">
</div>
<div class="form-group">
<label>Part Type </label>
<input type="text" class="form-control" name="part_type" placeholder="Part Type">
</div>
<div class="form-group">
<label>Supplier Name</label>
<select class="form-control" name="suppliers">
{% for supplier in suppliers %}
<option value="{{ supplier.id }}">{{ supplier.name }}</option>
{% endfor %}
</select>
</div>
</div>
<!-- /.card-body -->
<div class="card-footer">
<button type="submit" class="btn btn-primary">Add Part</button>
</div>
</form>
</div>
<!-- /.card -->
</div>
</div>
</div><!-- /.container-fluid -->
</section>
{% endblock main_content %}
Now the services in parts template does not show up at all. There is no choices on the form. But, for the add services template, it does populate. I have no idea why this happens because I have used the exact same code for both templates.
Changing the view to this solved the issue
def add_part(request):
parts = Parts.objects.all()
context = {
"suppliers": suppliers
}
return render(request, 'admintemplate/add_parts_template.html', context)

Django Form is not valid but not giving me an error

In the below form If I uncomment "Product" field my form is not working.I think there is something wrong with my html file. I am not including whole model code only the neccessary code.
lead/models.py
class Lead(models.Model):
description = models.TextField(blank=True, null=True)
assigned_to = models.ManyToManyField(User, related_name='lead_assigned_users')
account_name = models.CharField(max_length=255, null=True, blank=True)
tags = models.ManyToManyField(Tags, blank=True)
contacts = models.ManyToManyField(Contact, related_name="lead_contacts")
ctype = models.CharField(default=None,max_length=20, choices=TYPE, blank=True, null=True)
product = models.CharField(default=None,max_length=255, blank=False, null=True)
common/models.py
class Products(models.Model):
name = models.CharField(max_length=50,null=False,blank=False)
def __str__(self):
return self.name
lead/view.py
#login_required
def create_lead(request):
template_name = "create_lead.html"
if request.POST:
form = LeadForm(request.POST, request.FILES, assigned_to=users)
if form.is_valid():
lead_obj = form.save(commit=False)
lead_obj.created_by = request.user
lead_obj.save()
if request.POST.get('tags', ''):
tags = request.POST.get("tags")
splitted_tags = tags.split(",")
for t in splitted_tags:
tag = Tags.objects.filter(name=t)
if tag:
tag = tag[0]
else:
tag = Tags.objects.create(name=t)
lead_obj.tags.add(tag)
if request.POST.getlist('assigned_to', []):
lead_obj.assigned_to.add(*request.POST.getlist('assigned_to'))
assigned_to_list = request.POST.getlist('assigned_to')
if request.POST.getlist('products', []):
lead_obj.product.add(*request.POST.getlist('products'))
products = request.POST.getlist('products')
if request.POST.getlist('teams', []):
user_ids = Teams.objects.filter(id__in=request.POST.getlist('teams')).values_list('users', flat=True)
assinged_to_users_ids = lead_obj.assigned_to.all().values_list('id', flat=True)
for user_id in user_ids:
if user_id not in assinged_to_users_ids:
lead_obj.assigned_to.add(user_id)
current_site = get_current_site(request)
recipients = list(lead_obj.assigned_to.all().values_list('id', flat=True))
send_email_to_assigned_user.delay(recipients, lead_obj.id, domain=current_site.domain,
protocol=request.scheme)
if request.FILES.get('lead_attachment'):
attachment = Attachments()
attachment.created_by = request.user
attachment.file_name = request.FILES.get(
'lead_attachment').name
attachment.lead = lead_obj
attachment.attachment = request.FILES.get('lead_attachment')
attachment.save()
if request.POST.get('status') == "converted":
account_object = Account.objects.create(
created_by=request.user, name=lead_obj.account_name,
email=lead_obj.email, phone=lead_obj.phone,
description=request.POST.get('description'),
website=request.POST.get('website'),
)
account_object.billing_address_line = lead_obj.address_line
account_object.billing_street = lead_obj.street
account_object.billing_city = lead_obj.city
account_object.billing_state = lead_obj.state
account_object.billing_postcode = lead_obj.postcode
account_object.billing_country = lead_obj.country
for tag in lead_obj.tags.all():
account_object.tags.add(tag)
if request.POST.getlist('assigned_to', []):
# account_object.assigned_to.add(*request.POST.getlist('assigned_to'))
assigned_to_list = request.POST.getlist('assigned_to')
current_site = get_current_site(request)
recipients = assigned_to_list
send_email_to_assigned_user.delay(recipients, lead_obj.id, domain=current_site.domain,
protocol=request.scheme)
account_object.save()
success_url = reverse('leads:list')
if request.POST.get("savenewform"):
success_url = reverse("leads:add_lead")
return JsonResponse({'error': False, 'success_url': success_url})
return JsonResponse({'error': True, 'errors': form.errors})
context = {}
context["lead_form"] = form
context["accounts"] = Account.objects.filter(status="open")
context["users"] = users
context["countries"] = COUNTRIES
context["status"] = LEAD_STATUS
context["source"] = LEAD_SOURCE
context["ctype"] = TYPE
context["product"] = Products.objects.all()
context["assignedto_list"] = [int(i) for i in request.POST.getlist('assigned_to', []) if i]
return render(request, template_name, context)
create_lead.html
<form id="lead_form" method="POST" action="" novalidate enctype="multipart/form-data">
<div class="overview_form_block row marl justify-content-center">
<div class="col-md-9">
<div class="card">
<div class="card-body">
<div class="card-title text-center">
{% if lead_obj %}EDIT{% else %}CREATE{% endif %} LEAD
</div>
<div class="row marl no-gutters">
<div class="col-md-4">
<div class="filter_col col-md-12">
<div class="form-group">
<div class="row">
<div class="filter_col col-md-6">
<label for="exampleInputEmail1" class="name" >First Name{% if lead_form.first_name.field.required %}<span class="error">*</span>{% endif %}</label>
{{ lead_form.first_name }}
<span class="error error_message" id="error_id_first_name">{{ lead_form.first_name.errors }}</span>
</div>
<div class="filter_col col-md-6">
<label for="exampleInputEmail1" class="name" >Last Name{% if lead_form.last_name.field.required %}<span class="error">*</span>{% endif %}</label>
{{ lead_form.last_name }}
<span class="error error_message" id="error_id_last_name">{{ lead_form.last_name.errors }}</span>
</div>
</div>
</div>
</div>
<div class="filter_col col-md-12">
<div class="form-group">
<label for="exampleInputEmail1" {% if request.POST.status == "converted" %} class="required" {% endif %}>Account Name{% if lead_form.account_name.field.required %}<span class="error">*</span>{% endif %}</label>
{{ lead_form.account_name }}
<span class="error error_message" id="error_id_account_name">{{ lead_form.account_name.errors }}</span>
{% if error %}
<span class="error error_message" id="error_id_}">{{ error }}</span>
{% endif %}
</div>
</div>
<div class="filter_col col-md-12">
<div class="form-group">
<label for="exampleInputEmail1">Title{% if lead_form.title.field.required %}<span class="error">*</span>{% endif %}</label>
{{ lead_form.title }}
<span class="error error_message" id="error_id_title">{{ lead_form.title.errors }}</span>
</div>
</div>
<div class="filter_col col-md-12">
<div class="form-group ">
<label for="exampleInputEmail1">Phone{% if lead_form.phone.field.required %}<span class="error">*</span>{% endif %}</label>
{{ lead_form.phone }}
<span class="error error_message" id="error_id_phone">{{ lead_form.phone.errors }}</span>
</div>
</div>
<div class="filter_col col-md-12">
<div class="form-group ">
<label for="exampleInputEmail1" class="required">Email Address{% if lead_form.email.field.required %}<span class="error">*</span>{% endif %}</label>
{{ lead_form.email }}
<span class="error error_message" id="error_id_email">{{ lead_form.email.errors }}</span>
{% if error %}
<span class="error error_message" id="error_id_}">{{ error }}</span>
{% endif %}
</div>
</div>
<div class="filter_col col-md-12">
<div class="form-group">
<label for="exampleInputEmail1">Attachment</label>
<input type="file" name="lead_attachment" id="lead_attachment">
{% if lead_obj.lead_attachment.count %}
{% for attachment in lead_obj.lead_attachment.all %}
<div id="attachment{{attachment.id}}" class="mt-2">
<a target="_blank" href="{{ attachment.attachment.url }}">{{ attachment.file_name }}</a>
{% if not attachment.created_by.role == 'ADMIN' or attachment.created_by.role == request.user.role %}
<a class="action btn primary_btn" onclick="remove_attachment({{attachment.id}})">X</a>
{% endif %}
</div>
{% endfor %}
{% endif %}
<span class="error"></span>
</div>
</div>
</div>
<div class="col-md-4">
<div class="filter_col col-md-12">
<div class="form-group">
<label for="exampleInputEmail1">Website{% if lead_form.website.field.required %}<span class="error">*</span>{% endif %}</label>
{{ lead_form.website }}
<span class="error error_message" id="error_id_website">{{ lead_form.website.errors }}</span>
</div>
</div>
<div class="filter_col col-md-12">
<div class="form-group">
<label for="exampleInputEmail1" >Description{% if lead_form.description.field.required %}<span class="error">*</span>{% endif %}</label>
{{ lead_form.description }}
<span class="error error_message" id="error_id_description">{{ lead_form.description.errors }}</span>
</div>
</div>
{% if request.user.is_superuser or request.user.role == 'ADMIN' %}
<div class="filter_col col-12">
<div class="form-group">
<label for="id_sattus">Teams{% if lead_form.teams.field.required %}<span
class="error">*</span>{% endif %}</label>
{{ lead_form.teams }}
<span class="error" id="id__teams">{{ lead_form.teams.errors }}</span>
</div>
</div>
{% endif %}
<div class="filter_col col-md-12">
<div class="form-group">
<label for="exampleInputEmail1">Assigned Users</label>
<select class="assigned_users form-control" name="assigned_to" multiple="multiple">
{% for user in users %}
<option value="{{user.id}}" {% if user in lead_obj.assigned_to.all or user.id in assignedto_list %} selected="" {% endif %}>{{user.email}}</option>
{% endfor %}
</select>
<span class="error error_message" id="error_id_assigned_to">{{ lead_form.assigned_to.errors }}</span>
</div>
</div>
<div class="filter_col col-md-12">
<div class="form-group">
<label for="exampleInputEmail1"/>Status{% if lead_form.status.field.required %}<span class="error">*</span>{% endif %}</label>
{{ lead_form.status }}
<span class="error error_message" id="error_id_status">{{ lead_form.status.errors }}</span>
</div>
</div>
</div>
<div class="col-md-4">
<div class="filter_col col-md-12">
<div class="form-group">
<label for="exampleInputEmail1"/>Source{% if lead_form.source.field.required %}<span class="error">*</span>{% endif %}</label>
{{ lead_form.source }}
<span class="error error_message" id="error_id_source">{{ lead_form.source.errors }}</span>
</div>
</div>
<div class="filter_col col-md-12">
<div class="form-group">
<label for="exampleInputEmail1" >Address</label>
{{ lead_form.address_line }}
<span class="error error_message" id="error_id_address_line">{{ lead_form.address_line.errors }}</span>
</div>
{{ lead_form.street }}
<span class="error error_message" id="error_id_street">{{ lead_form.street.errors }}</span>
<div class="row" style="margin-top:10px;">
<div class="col-md-4">
{{ lead_form.city }}
<span class="error error_message" id="error_id_city">{{ lead_form.city.errors }}</span>
</div>
<div class="col-md-4">
{{ lead_form.state }}
<span class="error error_message" id="error_id_state">{{ lead_form.state.errors }}</span>
</div>
<div class="col-md-4">
{{ lead_form.postcode }}
<span class="error error_message" id="error_id_postcode">{{ lead_form.postcode.errors }}</span>
</div>
<div class="col-md-12" style="margin-top:10px;">
{{ lead_form.country }}
<span class="error error_message" id="error_id_country">{{ lead_form.country.errors }}</span>
</div>
</div>
</div>
<br>
<div class="filter_col col-12">
<div class="form-group">
<label>Tags</label>
<div class="txt-box-div" id="tag-div"><input type="text" name="tags" id="tags_1" value="{% for t in lead_obj.tags.all %}{{t.name}}, {% endfor %}" class="tags"/>
</div>
</div>
</div>
<div class="filter_col col-md-12">
<div class="form-group">
<label for="exampleInputEmail1"/>Type{% if lead_form.ctype.field.required %}<span class="error">*</span>{% endif %}</label>
{{ lead_form.ctype }}
<span class="error error_message" id="error_id_type">{{ lead_form.ctype.errors }}</span>
</div>
</div>
<div class="filter_col col-md-12">
<div class="form-group">
<label for="exampleInputEmail1">Products</label>
<select class="assigned_users form-control" name="products" multiple="multiple">
{% for pro in product %}
<option value="{{user.id}}" {% if pro in lead_obj.product.all %} selected="" {% endif %}>{{pro.name}}</option>
{% endfor %}
</select>
<span class="error error_message" id="error_id_products">{{ lead_form.product.errors }}</span>
</div>
</div>
</div>
<div class="col-md-12">
<div class="row marl buttons_row text-center form_btn_row">
<button class="btn btn-default save" type="submit" id="submit_btn">Save</button>
{% if not lead_obj %}
<button class="btn btn-success save savenew" type="submit">Save & New</button>
{% endif %}
Cancel
</div>
</div>
</div>
</div>
</div>`enter code here`
</div>
<input type="hidden" id="save_new_form" name="savenewform">
</div>
</form>
my ajax code
$('form#lead_form').ajaxForm({
type:'POST',
dataType:'json',
url: ".",
data:$('#lead_form').serialize(),
beforeSubmit: disableButton,
beforeSend: function() {
var percentVal = '0%';
bar.width(percentVal);
percent.html(percentVal);
},
uploadProgress: function(event, position, total, percentComplete) {
var percentVal = percentComplete + '%';
bar.width(percentVal);
if (percentVal == '100%'){
percent.html('Uploading ...')
}
else{
percent.html(percentVal);
}
},
success: function(data) {
if(data.error){
$('#progress_bar').hide()
$('.error_message').html('')
for (var key in data.errors) {
$('#error_id_'+key).html("<p>" + data.errors[key][0] + "</p>");
};
$('#submit_btn').removeAttr('disabled')
}
else{
window.location = data.success_url;
}
}
});
The form should be submitted successfully but, it's not working and not giving me an error.