models.py
class Subject(models.Model):
code = models.CharField(max_length = 31)
name = models.CharField(max_length = 31)
credit = models.CharField(max_length = 31)
def __unicode__(self):
return str(self.code) + "-"+ str(self.name)
class Student(models.Model):
name = models.CharField(max_length = 31)
roll = models.CharField(max_length = 31)
subject = models.ForeignKey(Subject, related_name = 'subjects')
marks = models.CharField(max_length = 31)
def __unicode__(self):
return str(self.name) + "-"+ str(self.roll)
views.py
def add_student(request):
subject = Subject.objects.all()
if request.method == "POST":
obj = Student()
obj.name = request.POST['name']
obj.roll = request.POST['roll']
obj.subject = Subject.objects.get(name = request.POST['subject'])
obj.marks = request.POST['marks']
obj.save()
var = RequestContext(request,{'title': ' Add Student','heading':'ADD STUDENT','subject':subject})
return render_to_response('student.html',var)
And template is the followings..
<form action="." method="POST">
<table>
<tr>
<td>Subject</td>
<td>
<select id="subject" name= "subject">
<option value="">- - Select - -</option>
{% for i in subject %}
<option>{{ i.name }}</option>
{% endfor %}
</select>
</td>
</tr>
<tr>
<td>Student Name</td>
<td>
<input id="name" type = "text" name ="name" />
</td>
</tr>
<tr>
<td>Student Roll</td>
<td>
<input id="roll" type = "text" name ="roll" />
</td>
</tr>
<tr>
<td>Marks</td>
<td>
<input id="marks" type = "text" name ="marks" />
</td>
</tr>
<tr>
<td>
<input id="save" type = "Submit" value="Submit" />
</td>
</tr>
</form>
</table>
Now how will be the jquery if i want to select one subject from the select box then the name, roll and marks will be loaded dynamically on the corresponding text box as described above with jquery.