Последние пару дней я боролся с этой проблемой. Моя модельная форма не отправляется. Данные продолжают появляться на html (где они отображаются после нажатия кнопки отправки без отправки. Я также проверил базу данных и заметил, что она не была сохранена.
views.py
def add_logbook(request):
if request.method == 'POST':
inventory = InventoryForm(request.POST)
if inventory.is_valid():
inventory.cleaned_data.get('entry_date')
updated_date = inventory.cleaned_data.get('updated_date')
file_no = inventory.cleaned_data.get('file_no')
case_no = inventory.cleaned_data.get('case_no')
court = inventory.cleaned_data.get('court')
court_no = inventory.cleaned_data.get('court_no')
male_suspect = inventory.cleaned_data.get('male_suspect')
female_suspect = inventory.cleaned_data.get('female_suspect')
male_underage_suspect = inventory.cleaned_data.get('male_underage_suspect')
female_underage_suspect = inventory.cleaned_data.get('female_underage_suspect')
assigned_to = inventory.cleaned_data.get('assigned_to')
agency = inventory.cleaned_data.get('agency')
inventory.save()
messages.success(request, 'Data Submited')
else:
inventory = InventoryForm()
data = {'invt':inventory}
return render(request, 'add-logbook.html', data)
ModelForm forms.py
class InventoryForm(forms.ModelForm):
AGENCY_CHOICE = [
('CA', ' Choose Agency'),
('NCS', 'NCS'),
('PL', 'Police'),
('DSS', 'DSS'),
('OT', 'Others'),
]
file_no = forms.CharField(required=False, widget=forms.TextInput(attrs={
'class':'form-control', 'placeholder':'File Number'}))
case_no = forms.CharField(required=False, widget=forms.TextInput(attrs={
'class':'form-control', 'placeholder':'Case Number'}))
court = forms.CharField(required=False, widget=forms.TextInput(attrs={
'class':'form-control', 'placeholder':'Court'}))
court_no = forms.CharField(required=False, widget=forms.TextInput(attrs={
'class':'form-control', 'placeholder':'Court Number'}))
male_suspect = forms.IntegerField(widget=forms.NumberInput(attrs={
'class':'form-control', 'placeholder':'Enter Suspect'}))
female_suspect = forms.IntegerField(widget=forms.NumberInput(attrs={
'class':'form-control', 'placeholder':'Enter Suspect'}))
male_underage_suspect = forms.IntegerField(widget=forms.NumberInput(attrs={
'class':'form-control', 'placeholder':'Enter Suspect'}))
female_underage_suspect = forms.IntegerField(widget=forms.NumberInput(attrs={
'class':'form-control', 'placeholder':'Enter Suspect'}))
assigned_to = forms.CharField(widget=forms.TextInput(attrs={
'class':'form-control', 'placeholder':'Enter Name'}))
agency = forms.CharField(widget=forms.Select(attrs={
'class':'form-control'}, choices=AGENCY_CHOICE))
class Meta():
model = InventoryModel
fields = '__all__'
Модель, связанная с формой models.py
class InventoryModel(models.Model):
NCS = 'NS'
POLICE = 'PL'
DSS = 'DS'
OTHERS = 'OT'
AGENCY_CHOICE = [
(NCS, 'NCS'),
(POLICE, 'Police'),
(DSS, 'DSS'),
(OTHERS, 'Others'),
]
# CASE DETAILS
entry_date = models.DateTimeField(auto_now_add=True, auto_now=False)
updated_date = models.DateTimeField(auto_now_add=False, auto_now=True)
file_no = models.CharField(max_length=25, blank=True)
case_no = models.CharField(max_length=25, blank=True)
court = models.CharField(max_length=100, blank=True)
court_no = models.CharField(max_length=15, blank=True)
male_suspect = models.IntegerField()
female_suspect = models.IntegerField()
male_underage_suspect = models.IntegerField()
female_underage_suspect = models.IntegerField()
assigned_to = models.CharField(max_length=150)
agency = models.CharField(max_length=4, choices=AGENCY_CHOICE)
def __str__(self):
return self.file_no