urls.py
from django.conf.urls import url
from django.contrib import admin
from son1.views import *
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^index/',index),
]
views.py
from django.shortcuts import render
# Create your views here.
from son1.models import *
def index(request):
list=Student.objects.all()
list1=Teacher.objects.all()
return render(request,'index.html',locals())
models.py
from __future__ import unicode_literals
from django.db import models
# Create your models here.
class Teacher(models.Model):
tname=models.CharField(max_length=50)
class Student(models.Model):
name=models.CharField(max_length=50)
teacher=models.ForeignKey(Teacher,related_name='s_t')
index.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title></title>
</head>
<body>
{% for foo in list1 %}
<br>
{{ foo.tname }}
<br>
{% for student in foo.s_t.all %}
{{ student.name }}
{% endfor %}
<br>
{% endfor %}
</body>
</html>