Django:“详细信息”:“未找到”。在网页上找不到HTTP 404

时间:2019-02-12 05:20:22

标签: python django

下面是我的模特:

class Grade(models.Model):
    grade = models.CharField(max_length=255, primary_key=True)

class Student(models.Model):
    name = models.CharField(max_length=255) 
    grade = models.ForeignKey(grade, on_delete=models.CASCADE)
    rollno = models.BigIntegerField()

views.py如下:

 class RegisterStudent(generics.ListAPIView):
    '''
    GET stu/
    POST stu/
    '''
    queryset = Student.objects.all()
    serializer_class = StudentSerializer

    def post(self, request, *args, **kwargs):
        grade, created = Grade.objects.get_or_create(
        grade=request.data["grade"],
    )
        a_site = Student.objects.create(
            name=request.data["name"],
            grade = grade,
            rollno=request.data["rollno"],
        )
        print(type(a_site.grade))
        return Response(
            data=StudentinGSerializer(a_site).data,
            status

class StudentinGrade(generics.RetrieveUpdateAPIView):
        print('You should be the one wrking')
        '''
        GET grade/{grade}/stu/
        POST grade/{grade}/stu/    
        '''
        queryset = Student.objects.all()
        serializer_class = StudentSerializer

        def post(self, request, *args, **kwargs):
            grade, created = Grade.objects.get_or_create(
            grade=request.data["grade"],
        )
            a_site = Student.objects.create(
                name=request.data["name"],
                grade = grade,
                rollno=request.data["rollno"],
            )
            return Response(
                data=DeviceSerializer(a_site).data,
                status=status.HTTP_201_CREATED
            )

我想对头等舱做的只是获取学生的数据,这是有效的。

urlpatterns = [
   path('grade/<str:pk>/stu', StudentinGrade.as_view(), name = "grade-stu"),]

当我第一次加载页面时,它显示:“详细信息”:“未找到”。不知道为什么,因为当我发布时它接受了并且可以看到更改。

我已阅读以下链接,但仍不确定: https://docs.djangoproject.com/en/2.1/topics/http/urls/

任何帮助将不胜感激。谢谢

2 个答案:

答案 0 :(得分:0)

DRF在get()中不允许使用UpdateAPIView方法。它提供了putpatch方法处理程序。要检索和更新,您可以使用RetrieveUpdateAPIView

按照文档Here

答案 1 :(得分:0)

这对我有用:

class StudentinGrade(generics.ListCreateAPIView):
        print('You should be the one wrking')
        '''
        GET grade/{grade}/stu/
        POST grade/{grade}/stu/    
        '''
        queryset = Student.objects.all()
        serializer_class = StudentSerializer

        def post(self, request, *args, **kwargs):
            g1= Grade.objects.get(
        grade=kwargs["pk"],
    )
            a_site = Student.objects.create(
            name=request.data["name"],
            grade = g1,
            rollno=request.data["rollno"],
        )
            return Response(
            data=StudentinGSerializer(a_site).data,
            status=status.HTTP_201_CREATED
        )

我使用了错误的视图类型并错误地获取了数据。