I am new to django. I am creating a simple CRUD application using django 2.2, I have a simple addstudent.html page where i can add data in some fields including a dropdown where I can select the class of student and database is updated. Below is my code: forms.py
class StudentForm(forms.ModelForm):
class_list= [
('Class IIX', 'Class IIX'),
('Class IX', 'Class IX'),
('Class X', 'Class X'),
('Class XI', 'Class XI'),
('Class XII', 'Class XII')
]
student_class= forms.CharField(label='Class', widget=forms.Select(choices=class_list,attrs={'style': 'width:175px'}))
class Meta:
model=Student
fields='__all__'
models.py
class Student(models.Model):
student_id=models.CharField(max_length=20)
student_name=models.CharField(max_length=100)
student_email=models.EmailField()
student_contact=models.CharField(max_length=100)
student_class=models.CharField(max_length=100)
class Meta:
db_table='student'
snippet of addstudent.html template where dropdown is shown correctly
<div class="form-group row">
<label for="sname" class="col-sm-4 col-form-label">Student Class:</label>
<div class="col-sm-8">
{{form.student_class}}
</div>
</div>
Problem: I am not able to figure out how to preselect the dropdown when I edit a student, I get the value as 'Class X' from the database but how do i use this database value to select correct option in dropdown?
Please Note: This project is for school kids and I can't use JQUERY/JAVASCRIPT here.