I am new to Django. Trying to add data to database using ModelForm. But getting an error "This field is required.."
My model:
class Docstest(models.Model):
uploader =models.ForeignKey("auth.User",blank=False,null=False,on_delete=models.CASCADE)
nameOfApplicant = models.CharField(max_length=50,blank=False)
serviceRequestedName= models.ForeignKey(ServicesOffered,on_delete=models.CASCADE,blank=False,null=False)
nameOfDocument = models.ForeignKey(DocumentsRequired,on_delete=models.CASCADE,blank=False,null=False)
documentFile = models.FileField(upload_to='documents/%M', blank = False, null = False)
def __str__(self):
return str(self.nameOfDocument) str(self.uploader)
forms.py
class MyForm(forms.ModelForm):
class Meta:
model = Docstest
fields = ["uploader", "nameOfApplicant","serviceRequestedName","nameOfDocument","documentFile",]
labels = {"uploader":"uploader", "nameOfApplicant":"nameOfApplicant","serviceRequestedName":"serviceRequestedName","nameOfDocument":"nameOfDocument","documentFile":"documentFile",}
my html
{% extends 'base.html' %}
{% block columncard %}
<div >
<form method="POST">
<fieldset>
<legend>Form</legend>
{% csrf_token %}
{{ form.as_p }}
<button type="submit" >Submit</button>
</fieldset>
</form>
</div>
{% endblock %}
There is a error "This field is required". Data not saved in Database. Help.
CodePudding user response:
I think you need to add enctype="multipart/form-data"
to your form tag in your template