I am trying to save data from HTML Form to SQLite database. My database is connected to my app and project. I am able to enter from Django Admin, but my values from Input tag are not going in database.
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'
This is the error that I am having. NOT NULL constraint failed: main_paddy_purchase.token_no
Here is a screen shot. Error Image
CodePudding user response:
Your problem is that token_no
is missing in request.POST
.
You are using everywhere request.POST.get(key)
which when isn't able find the key - returns None
.
Probably you forgot to add corresponding input
field with a proper name.
I suggest you take a look into Django Forms. And use them to validate the input of user and save changes into the database. The code will also look much smoother.
You can keep your models.py. Just rename the model to PaddyPurchase
according to the naming conventions in python.
Then you would need to create ModelForm from your model, which will automatically take all the fields:
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",
)
Then you could use the form in your 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 can be also used to render the form with all the neccessary inputs to the view, I suggest you try it. In order to do it, simply add {{form}}
into your <form ...> </form>
. (Works only when you provided form
in render function)
You can read more about django forms in official documentation
CodePudding user response:
@Volodymyr Piskun
Thanks for your reply. I tried using Django Forms and it is working now. Thanks a lot. It was really helpful.