HTML 输入标记值未保存在 SQLite 数据库中 - Django
HTML Input Tage Values not saving in SQLite Database - Django
我正在尝试将数据从 HTML 表单保存到 SQLite 数据库。我的数据库已连接到我的应用程序和项目。我可以从 Django Admin 进入,但是我输入标签的值没有进入数据库。
Views.py
def add_record(request):
if request.method == 'POST':
ref_no = request.POST.get('ref_no')
token_no = request.POST.get('token_no')
agent_name = request.POST.get('agent_name')
trip_no = request.POST.get('trip_no')
date = request.POST.get('date')
vehicle_no = request.POST.get('vehicle_no')
bora = request.POST.get('bora')
katta = request.POST.get('katta')
plastic = request.POST.get('plastic')
farmer_name = request.POST.get('farmer_name')
farmer_address = request.POST.get('farmer_address')
farmer_mob = request.POST.get('farmer_mob')
gross_weight = request.POST.get('gross_weight')
tier_weight = request.POST.get('tier_weight')
net_weight = request.POST.get('net_weight')
bora_weight = request.POST.get('bora_weight')
suddh_weight = request.POST.get('suddh_weight')
loading = request.POST.get('loading')
unloading = request.POST.get('unloading')
unloading_point = request.POST.get('unloading_point')
dharamkanta_man = request.POST.get('daramkanta_man')
rate = request.POST.get('rate')
bardana = request.POST.get('rate')
gross_total = request.POST.get('gross_total')
deduction = request.POST.get('deduction')
kanta = request.POST.get('kanta')
hemali = request.POST.get('hemali')
our_vehicle_rent = request.POST.get('our_vehicle_rent')
agent_commission = request.POST.get('agent_commission')
other_amt = request.POST.get('other_amt')
other_remarks = request.POST.get('other_remarks')
advance = request.POST.get('advance')
net_total = request.POST.get('net_total')
var_datasave = paddy_purchase(ref_no=ref_no,
token_no=token_no,
agent_name=agent_name,
trip_no=trip_no,
date=date,
vehicle_no=vehicle_no,
bora=bora,
katta=katta,
plastic=plastic,
farmer_name=farmer_name,
farmer_address=farmer_address,
farm_mob=farmer_mob,
gross_weight=gross_weight,
tier_weight=tier_weight,
net_weight=net_weight,
bora_weight=bora_weight,
suddh_weight=suddh_weight,
loading=loading,
unloading=unloading,
unloading_point=unloading_point,
dharamkanta_man=dharamkanta_man,
rate=rate,
bardana=bardana,
gross_total=gross_total,
deduction=deduction,
kanta=kanta,
hemali=hemali,
our_vehicle_rent=our_vehicle_rent,
agent_commission=agent_commission,
other_amt=other_amt,
other_remarks=other_remarks,
advance=advance,
net_total=net_total
)
var_datasave.save()
messages.success(request, 'Record saved successfully.')
return redirect('index')
return render(request, 'add.html')
Models.py
class paddy_purchase(models.Model):
ref_no = models.IntegerField(primary_key='true')
token_no = models.CharField(max_length=20, unique='true')
agent_name = models.CharField(max_length=20)
trip_no = models.IntegerField()
date = models.DateField()
vehicle_no = models.CharField(max_length=10)
bora = models.IntegerField()
katta = models.IntegerField()
plastic = models.IntegerField()
farmer_name = models.CharField(max_length=30)
farmer_address = models.CharField(max_length=40)
farm_mob = models.CharField(max_length=10)
gross_weight = models.IntegerField()
tier_weight = models.IntegerField()
net_weight = models.IntegerField()
bora_weight = models.IntegerField()
suddh_weight = models.FloatField()
loading = models.IntegerField()
unloading = models.IntegerField()
unloading_point = models.CharField(max_length=20)
dharamkanta_man = models.CharField(max_length=10)
rate = models.IntegerField()
bardana = models.CharField(max_length=7)
gross_total = models.IntegerField()
deduction = models.IntegerField()
kanta = models.IntegerField()
hemali = models.IntegerField()
our_vehicle_rent = models.IntegerField()
agent_commission = models.IntegerField()
other_amt = models.IntegerField()
other_remarks = models.CharField(max_length=50)
advance = models.IntegerField()
net_total = models.IntegerField()
# For returning data in ADMIN SITE
def __str__(self):
return 'paddy_purchase'
这是我遇到的错误。
NOT NULL 约束失败:main_paddy_purchase.token_no
这是一个屏幕截图。
Error Image
您的问题是 request.POST
中缺少 token_no
。
您到处都在使用 request.POST.get(key)
,但找不到密钥 - returns None
。
可能您忘记添加具有适当名称的相应 input
字段。
我建议您看一下 Django Forms。并使用它们来验证用户的输入并将更改保存到数据库中。代码看起来也会流畅很多。
您可以保留您的 models.py。只需将模型重命名为 PaddyPurchase
according to the naming conventions in python.
Then you would need to create ModelForm from your model,会自动取所有字段:
forms.py
class PaddyPurchaseForm(ModelForm):
class Meta:
model = PaddyPurchase
fields = (
"ref_no",
"token_no",
"agent_name",
"trip_no",
"date",
"vehicle_no",
"bora",
"katta",
"plastic",
"farmer_name",
"farmer_address",
"farm_mob",
"gross_weight",
"tier_weight",
"net_weight",
"bora_weight",
"suddh_weight",
"loading",
"unloading",
"unloading_point",
"dharamkanta_man",
"rate",
"bardana",
"gross_total",
"deduction",
"kanta",
"hemali",
"our_vehicle_rent",
"agent_commission",
"other_amt",
"other_remarks",
"advance",
"net_total",
)
然后您可以在 views.py:
中使用表格
from .forms import PaddyPurchaseForm
...
def add_record(request):
if request.method == 'POST':
form = PaddyPurchaseForm(request.POST))
if form.is_valid():
messages.success(request, 'Record saved successfully.')
return redirect('index')
else:
form = PaddyPurchaseForm()
return render(request, 'add.html', {'form':form})
Django Forms 也可用于呈现包含所有必需的视图输入的表单,我建议您尝试一下。为此,只需将 {{form}}
添加到您的 <form ...> </form>
中。 (仅当您在渲染函数中提供 form
时有效)
您可以阅读更多关于 django forms in official documentation
我正在尝试将数据从 HTML 表单保存到 SQLite 数据库。我的数据库已连接到我的应用程序和项目。我可以从 Django Admin 进入,但是我输入标签的值没有进入数据库。
Views.py
def add_record(request):
if request.method == 'POST':
ref_no = request.POST.get('ref_no')
token_no = request.POST.get('token_no')
agent_name = request.POST.get('agent_name')
trip_no = request.POST.get('trip_no')
date = request.POST.get('date')
vehicle_no = request.POST.get('vehicle_no')
bora = request.POST.get('bora')
katta = request.POST.get('katta')
plastic = request.POST.get('plastic')
farmer_name = request.POST.get('farmer_name')
farmer_address = request.POST.get('farmer_address')
farmer_mob = request.POST.get('farmer_mob')
gross_weight = request.POST.get('gross_weight')
tier_weight = request.POST.get('tier_weight')
net_weight = request.POST.get('net_weight')
bora_weight = request.POST.get('bora_weight')
suddh_weight = request.POST.get('suddh_weight')
loading = request.POST.get('loading')
unloading = request.POST.get('unloading')
unloading_point = request.POST.get('unloading_point')
dharamkanta_man = request.POST.get('daramkanta_man')
rate = request.POST.get('rate')
bardana = request.POST.get('rate')
gross_total = request.POST.get('gross_total')
deduction = request.POST.get('deduction')
kanta = request.POST.get('kanta')
hemali = request.POST.get('hemali')
our_vehicle_rent = request.POST.get('our_vehicle_rent')
agent_commission = request.POST.get('agent_commission')
other_amt = request.POST.get('other_amt')
other_remarks = request.POST.get('other_remarks')
advance = request.POST.get('advance')
net_total = request.POST.get('net_total')
var_datasave = paddy_purchase(ref_no=ref_no,
token_no=token_no,
agent_name=agent_name,
trip_no=trip_no,
date=date,
vehicle_no=vehicle_no,
bora=bora,
katta=katta,
plastic=plastic,
farmer_name=farmer_name,
farmer_address=farmer_address,
farm_mob=farmer_mob,
gross_weight=gross_weight,
tier_weight=tier_weight,
net_weight=net_weight,
bora_weight=bora_weight,
suddh_weight=suddh_weight,
loading=loading,
unloading=unloading,
unloading_point=unloading_point,
dharamkanta_man=dharamkanta_man,
rate=rate,
bardana=bardana,
gross_total=gross_total,
deduction=deduction,
kanta=kanta,
hemali=hemali,
our_vehicle_rent=our_vehicle_rent,
agent_commission=agent_commission,
other_amt=other_amt,
other_remarks=other_remarks,
advance=advance,
net_total=net_total
)
var_datasave.save()
messages.success(request, 'Record saved successfully.')
return redirect('index')
return render(request, 'add.html')
Models.py
class paddy_purchase(models.Model):
ref_no = models.IntegerField(primary_key='true')
token_no = models.CharField(max_length=20, unique='true')
agent_name = models.CharField(max_length=20)
trip_no = models.IntegerField()
date = models.DateField()
vehicle_no = models.CharField(max_length=10)
bora = models.IntegerField()
katta = models.IntegerField()
plastic = models.IntegerField()
farmer_name = models.CharField(max_length=30)
farmer_address = models.CharField(max_length=40)
farm_mob = models.CharField(max_length=10)
gross_weight = models.IntegerField()
tier_weight = models.IntegerField()
net_weight = models.IntegerField()
bora_weight = models.IntegerField()
suddh_weight = models.FloatField()
loading = models.IntegerField()
unloading = models.IntegerField()
unloading_point = models.CharField(max_length=20)
dharamkanta_man = models.CharField(max_length=10)
rate = models.IntegerField()
bardana = models.CharField(max_length=7)
gross_total = models.IntegerField()
deduction = models.IntegerField()
kanta = models.IntegerField()
hemali = models.IntegerField()
our_vehicle_rent = models.IntegerField()
agent_commission = models.IntegerField()
other_amt = models.IntegerField()
other_remarks = models.CharField(max_length=50)
advance = models.IntegerField()
net_total = models.IntegerField()
# For returning data in ADMIN SITE
def __str__(self):
return 'paddy_purchase'
这是我遇到的错误。 NOT NULL 约束失败:main_paddy_purchase.token_no
这是一个屏幕截图。 Error Image
您的问题是 request.POST
中缺少 token_no
。
您到处都在使用 request.POST.get(key)
,但找不到密钥 - returns None
。
可能您忘记添加具有适当名称的相应 input
字段。
我建议您看一下 Django Forms。并使用它们来验证用户的输入并将更改保存到数据库中。代码看起来也会流畅很多。
您可以保留您的 models.py。只需将模型重命名为 PaddyPurchase
according to the naming conventions in python.
Then you would need to create ModelForm from your model,会自动取所有字段:
forms.py
class PaddyPurchaseForm(ModelForm):
class Meta:
model = PaddyPurchase
fields = (
"ref_no",
"token_no",
"agent_name",
"trip_no",
"date",
"vehicle_no",
"bora",
"katta",
"plastic",
"farmer_name",
"farmer_address",
"farm_mob",
"gross_weight",
"tier_weight",
"net_weight",
"bora_weight",
"suddh_weight",
"loading",
"unloading",
"unloading_point",
"dharamkanta_man",
"rate",
"bardana",
"gross_total",
"deduction",
"kanta",
"hemali",
"our_vehicle_rent",
"agent_commission",
"other_amt",
"other_remarks",
"advance",
"net_total",
)
然后您可以在 views.py:
中使用表格from .forms import PaddyPurchaseForm
...
def add_record(request):
if request.method == 'POST':
form = PaddyPurchaseForm(request.POST))
if form.is_valid():
messages.success(request, 'Record saved successfully.')
return redirect('index')
else:
form = PaddyPurchaseForm()
return render(request, 'add.html', {'form':form})
Django Forms 也可用于呈现包含所有必需的视图输入的表单,我建议您尝试一下。为此,只需将 {{form}}
添加到您的 <form ...> </form>
中。 (仅当您在渲染函数中提供 form
时有效)
您可以阅读更多关于 django forms in official documentation