获取没有任何记录的表

时间:2016-06-24 12:55:12

标签: angularjs rest django-rest-framework

我是Django REST和Angular JS的新手,所以我试图理解它们背后的概念(以及两者之间的集成)。

我正在制作一个应该打印课堂和学生列表的REST API,这是我目前的工作

models.py

class Classroom(models.Model):
    school = models.ForeignKey(School, related_name='schools')
    academic_year = models.CharField(max_length=9)
    classroom = models.CharField(max_length=100)
    floor = models.IntegerField(default=0)

    class Meta:
        unique_together = (('school', 'classroom', 'academic_year'),)

class Student(models.Model):
    classroom = models.ForeignKey(Classroom, related_name='students')
    first_name = models.CharField(max_length=40)
    last_name = models.CharField(max_length=60)
    GENDER_CHOICES = (
        ('M', 'Male'),
        ('F', 'Female'),
    )
    gender = models.CharField(max_length=1, choices=GENDER_CHOICES)
    birthday = models.DateField()

serializers.py

class StudentSerializer(serializers.ModelSerializer):   
    class Meta:
        model = Student
        fields = ('classroom', 'first_name', 'last_name', 'gender', 'birthday')

class ClassroomSerializer(serializers.ModelSerializer):
    students = StudentSerializer(many=True)

    class Meta:
        model = Classroom
        depth = 1
        fields = ('school', 'academic_year', 'classroom', 'floor', 'students')

views.py

def index(request):
    return render_to_response('school_app/base.html', RequestContext(request))

class StudentViewSet(viewsets.ModelViewSet):
    queryset = Student.objects.all()
    serializer_class = StudentSerializer

class ClassroomViewSet(viewsets.ModelViewSet):
    queryset = Classroom.objects.all()
    serializer_class = ClassroomSerializer

和urls.py

router = routers.SimpleRouter()

router.register(r'classrooms', ClassroomViewSet)
router.register(r'students', StudentViewSet)

urlpatterns = router.urls

urlpatterns = [
    url(r'^admin/', include(admin.site.urls)),
    url(r'^api-auth/', include('rest_framework.urls', namespace='rest_framework')),
    url(r'^api/classrooms/$', school_app.views.ClassroomViewSet.as_view({'get': 'list'}), name='classroom-list'),
    url(r'^api/students/$', school_app.views.StudentViewSet.as_view({'get': 'list'}), name='student-list'),
    url(r'^', 'school_app.views.index', name='index-page'),
]

这是该项目的Angular“部分”,其中包括:

base.html文件

<body ng-app="userListApp" ng-controller="UsersController">

    <table class="table table-striped">
        <thead>
            <tr>
                <th>Classroom</th>
                <th>School</th>
                <th>Academic year</th>
            </tr>
      </thead>
      <tbody>
          <tr ng-repeat="classroom in classrooms">
              <td>{{classroom.classroom}}</td>
              <td>{{classroom.school}}</td>
              <td>{{classroom.academic_year}}</td>
          </tr>
      </tbody>
    </table>

</body>

和app.js

var userListApp = angular.module('userListApp', ['ngResource']);

userListApp.controller('UsersController', function ($scope, Classroom) {

    Classroom.query().then(function (result) {
        console.log(result);
            $scope.classrooms = result.classrooms;
            }, function (result) {
                alert("Error: No data returned");
            });
        });

userListApp.factory('Classroom', [
  '$resource', function($resource) {
      return $resource('/api/classrooms/:id', {
          classrooms: '@classrooms'
    });
  }
]);

我认为脚本中有问题,但我无法意识到我错过了哪一部分。我也不确定路由。 我只得到没有记录的桌子。有什么想法吗?

0 个答案:

没有答案