I try to develop a simple input form to save a deposit for a fishing vessel. The vessel and the net are tables in the database. There is no error when the form is submitted but there is nothing happening in the background. I use a PostgreSQL database with PgAdmin for insights.I am a little bit stuck since it's my first time working with Django.
I tried adding the dep_id field into the form but it did not change anything.
[forms.py]
from django import forms
from django.forms import ModelForm
from myapp.models import Deposit
class UploadForm(ModelForm):
dep_date = forms.DateField()
harbour = forms.CharField()
vessel = forms.ModelChoiceField(queryset=Vessel.objects.all())
net = forms.ModelChoiceField(queryset=Net.objects.all())
amount = forms.DecimalField()
class Meta:
model = Deposit
fields = ['dep_date', 'harbour',
'vessel', 'net',
'amount']
[models.py]
from django.db import models
class Vessel(models.Model):
VID = models.IntegerField(primary_key=True, default=None)
vessel_name = models.CharField(max_length=100)
flag = models.CharField(max_length=100)
registration_number = models.CharField(max_length=100)
WIN = models.CharField(max_length=100)
IRCS = models.CharField(max_length=100)
vessel_type = models.CharField(max_length=250)
fishing_methods = models.CharField(max_length=255)
length = models.DecimalField(default=0, max_digits=5, decimal_places=2)
auth_period_from = models.CharField(max_length=100)
auth_period_to = models.CharField(max_length=100)
class Net(models.Model):
net_id = models.IntegerField(primary_key=True, default = None)
prod_date = models.DateField()
weight = models.DecimalField(default=0, max_digits=6, decimal_places=2)
material = models.CharField(max_length=100)
fishing_type = models.CharField(max_length=100, default=None)
class Deposit(models.Model):
dep_id = models.BigAutoField(primary_key=True, default=None)
dep_date = models.DateField()
harbour = models.CharField(max_length=100)
vessel = models.ForeignKey(Vessel, to_field='VID', on_delete=models.CASCADE)
net = models.ForeignKey(Net, to_field='net_id', on_delete=models.CASCADE)
amount = models.DecimalField(default=0, max_digits=5, decimal_places=2)
[views.py]
from django.shortcuts import render, redirect
from .models import Vessel
from .forms import UploadForm
def put_deposit(request):
if request.POST:
form = UploadForm(request.POST)
print(request)
if form.is_valid():
form.save()
redirect(index)
return render(request, 'upload.html', {'form' : UploadForm})
[upload.html]
<p> Upload </p>
<form method="POST" action="{% url 'put_deposit' %}" enctype="multipart/form-data">
{% csrf_token %}
{{form}}
<button> Submit </button>
</form>
Maybe I have any kind of dependency wrong or is it a problem with a key?
CodePudding user response:
You're using a ModelForm, but then adding fields which are already in your model to the form as if it were a regular form. You can add extra fields to your ModelForm by doing so, but since you are adding the same fields, perhaps that is why it is not validating.
Suggestion: Try
(1) What @Milo has already suggested to print out form.errors
to see if the form is indeed valid or not,
(2) Change your form to:
class UploadForm(ModelForm):
class Meta:
model = Deposit
fields = ['dep_date', 'harbour',
'vessel', 'net',
'amount']
(3) Also, although I do not think this is what is causing the error, your view has some issues. Try:
def put_deposit(request):
form = UploadForm(request.POST or None)
print(request.POST)
if form.is_valid():
form.save()
redirect('index')
else:
print(form.errors)
return render(request, 'upload.html', {'form' : form})
CodePudding user response:
This is more of a troubleshooting suggestion, but hard to show in a comment. Your form might not be validating for some reason or another - add this to see if there are errors:
def put_deposit(request):
if request.POST:
form = UploadForm(request.POST)
print(request)
if form.is_valid():
form.save()
else:
print(form.errors) # add these two lines
redirect(index)
return render(request, 'upload.html', {'form' : UploadForm})