How can i save the form and add the course name and student name in the views.
models.py
class Attendence (models.Model):
date = models.DateField(default=datetime.now, blank=True)
course = models.ForeignKey(Class, null=True, on_delete=models.CASCADE)
student = models.ForeignKey(Class_member, null=True, on_delete=models.CASCADE)
attendence = models.BooleanField(default=False)
class Course_member (models.Model):
student = models.ForeignKey(Student, null=True, on_delete=models.SET_NULL)
course = models.ForeignKey(Course, null=True, on_delete=models.CASCADE)
class Course (models.Model):
name = models.CharField(max_length=200)
teacher = models.ForeignKey(Techer, null=True, on_delete=models.SET_NULL)
views.py
def attendence (request, cource_id):
course = Course.objects.get(id=course_id)
students = Course_member.objects.filter(course=course)
attendence_list = Attendence.objects.filter(course=course)
form = attendence_form()
if request.method == 'POST':
if form.is_valid():
form.save()
return redirect ('db_app:courses')
context = {'attendence':attendence_list, 'form':form, 'students':students, 'course':course}
return render(request, 'db_app/attendence_course.html', context)
form.py
class Attendence_form(forms.ModelForm):
class Meta:
model = Attendence
fields = ['date','student','course','attendence']
labels = {'attendence':'',}
template.html
<form action="{% url 'db_app:attendence' course.id %}" method="POST">
<table class="table">
<thead>
<tr>
<th >Name</th>
<th >{{form.date}}</th>
</tr>
</thead>
<tbody>
{% for student in students %}
<tr>
<td> {{ student }} </td>
<td> {{ form.attendence | as_crispy_field }} </td>
</tr>
{% endfor %}
</tbody>
</table>
<button class="btn btn-primary" name="submit">Save</button>
</form>