I am trying to hide the submitted by field in my forms.py because I don't want user to upload the assignment on the behalf of some other user. So what I am doing is I am hiding the field but before hiding I am setting the value of logged in user to that input text using javascript but I am getting this error. (Hidden field submitted_by) This field is required. forms.py
class assignmentUploadForm(ModelForm):
class Meta:
model = Submissions
fields = ('submitted_by', 'submitted_to',
'submission_title', 'submission_file', 'submission_status')
widgets = {
'submitted_by': forms.TextInput(attrs={'class': 'form-control', 'type': 'hidden', 'id': 'user', 'value': ''}),
'submitted_to': forms.Select(attrs={'class': 'form-control'}),
'submission_title': forms.Select(attrs={'class': 'form-control'}),
}
template
<form method="post" enctype="multipart/form-data" style="margin-left: 240px;">
{% csrf_token %}
{{form.as_p}}
<input type="submit">
var name = "{{user.username}}"
document.getElementById('user').value = name;
views.py
class AddAssignmentView(CreateView):
model = Submissions
form_class = assignmentUploadForm
template_name = 'project/assignment.html'