Ожидаемое значение: строка 1 столбец 1 (char 0) Django форма
Я получаю ошибку Expecting value: line 1 column 1 (char 0)
при попытке добавить новую компанию с помощью формы Django. Я не смог найти дублирующие вопросы в google, поэтому хочу попросить любой помощи. Мои файлы ниже
model.py
class Company(models.Model):
id = models.AutoField(primary_key=True)
name = models.CharField(max_length=300, unique=True, blank=False, null=False)
domains = ArrayField(models.CharField(max_length=100))
licence_type = models.CharField(max_length=20, default='Demo')
status = models.CharField(max_length=20, default='Passive')
sector = models.CharField(max_length=100)
security_score = models.IntegerField(default=0)
licence_start_date = models.DateTimeField()
licence_end_date = models.DateTimeField()
insert_date = models.DateTimeField(default=timezone.now)
def __str__(self):
return self.name
forms.py
class AddCompanyForm(forms.ModelForm):
def __init__(self, *args, **kwargs):
super(AddCompanyForm, self).__init__(*args, **kwargs)
class Meta:
model = Company
exclude = ['security_score', 'insert_date', 'licence_start_date', 'licence_end_date']
name = forms.CharField(
required=True,
widget=forms.TextInput(
attrs={
"class": "form-control",
"id": "name",
"placeholder": "Company Name"
}
))
sector = forms.CharField(
required=False,
widget=forms.TextInput(
attrs={
"class": "form-control",
"id": "sector",
"placeholder": "Company Sector"
}
))
domains = forms.CharField(
required=True,
widget=forms.Textarea(
attrs={
"class": "form-control",
"id": "domains",
"rows": "4",
"placehoder":"Separate domains with comma (,)"
}
))
licence_date = forms.CharField(
required=True,
widget=forms.TextInput(
attrs={
"class": "form-control",
"id": "licence_date",
}
))
licence_type = forms.ChoiceField(
choices = [
('Demo', 'Demo'),
('Basic', 'Basic'),
('Pro', 'Pro'),
('Enterprise', 'Enterprise')],
required=True,
widget=forms.Select(
attrs={
"class":"form-control select2",
"id":"licence_type"
}
))
status = forms.ChoiceField(
choices = [
('Passive', 'Passive'),
('Active', 'Active')],
required=True,
widget=forms.Select(
attrs={
"class":"form-control select2",
"id":"status"
}
))
def clean_name(self):
data = self.cleaned_data['name']
if Company.objects.filter(name=data).count() > 0:
raise forms.ValidationError("{} Company already exists".format(data))
return data
views.py
Когда я заменяю AddCompanyForm на AddUserForm, ошибка больше не появляется. Поэтому я думаю, что проблема в моем классе формы.
def add_company(request):
form = AddCompanyForm(request.POST or None)
if request.method == "POST":
if form.is_valid():
licence_datetime = request.POST.get('licence_datetime')
split_licence_time = licence_datetime.split(' - ')
start_time = split_licence_time[0].strip() #company licence start time
end_time = split_licence_time[1].strip() #company licence end time
start_time = datetime.strptime(start_time, '%m/%d/%Y')
end_time = datetime.strptime(end_time, '%m/%d/%Y')
form = AddCompanyForm(request.POST)
company = form.save(commit=False)
company.licence_start_date = start_time
company.licence_end_date = end_time
form.save()
messages.add_message(request, messages.INFO, f'Company {name} added successfully')
return http.HttpResponseRedirect(reverse('list_company'))
else:
print(form.errors)
context = {
"form":form,
"segment":['company', 'admin_settings']
}
return render(request, 'account/company/add.html', context)
add.html
<form action="" method="post">
{% csrf_token %}
<div class="row">
<div class="col-md-6">
<div class="form-group">
<label for="name">Company Name</label>
{{ form.name }}
</div>
</div>
<div class="col-md-6">
<div class="form-group">
<label for="companySector">Sector</label>
{{ form.sector }}
</div>
</div>
</div>
<div class="form-group">
<label for="domains">Domains</label>
{{ form.domains }}
</div>
<div class="form-group">
<label>Licence Start-End Datetime</label>
<div class="input-group">
<div class="input-group-prepend">
<span class="input-group-text"><i class="far fa-clock"></i></span>
</div>
{{ form.licence_date }}
</div>
</div>
<div class="row">
<div class="col-md-6">
<div class="form-group">
<label>Licence Type</label>
{{ form.licence_type }}
</div>
</div>
<div class="col-md-6">
<div class="form-group">
<label>Status</label>
{{ form.status }}
</div>
</div>
</div>
<div class="card-footer float-right">
<button type="submit" class="btn btn-primary">Add Company</button>
</div>
</form>