我收到此错误:
Method Not Allowed: /api/v1/auth/users/student/submission/
"POST /api/v1/auth/users/student/submission/ HTTP/1.1" 405 41
当访问我的 Django 应用程序 modelviewset 之一的端点时(提交)。
以下是设置;
浏览量
class SubmissionViewSet(viewsets.ModelViewSet):
serializer_class = SubmissionSerializer
queryset = Submission.objects.all().order_by("-created_at")
permission_classes = [IsAuthenticated]
def perform_create(self, serializer):
serializer.save(student=self.request.user)
网址
router = routers.DefaultRouter()
router.register("admin/courses", views.AdminCourseViewSet)
router.register("student/courses", views.StudentCourseViewSet)
router.register("student/submission", views.SubmissionViewSet)
urlpatterns = [
path("users/", include(router.urls)),
]
序列化器
class SubmissionSerializer(serializers.ModelSerializer):
project = serializers.UUIDField()
course = serializers.SerializerMethodField()
date_submitted = serializers.SerializerMethodField()
class Meta:
model = Submission
fields = [
"id",
"project",
"course",
"submission_link",
"status",
"date_submitted",
"student_comments",
"mentor_comments",
]
read_only_fields = ["id","course", "status", "date_submitted"]
def get_course(self, instance):
return instance.project.course.title
def get_date_submitted(self, instance):
return instance.date_submitted.strftime("%b %d, %Y")
型号
class Submission(TimeStampedUUIDModel):
class STATUS(models.TextChoices):
"""
Choices for the project
"""
REVIEWED = "Reviewed", _("Reviewed")
PENDING = "Pending", _("Pending")
NOT_SUBMITTED = "Not_Submitted", _("Not_Submitted")
student = models.ForeignKey(User, on_delete=models.CASCADE)
project = models.ForeignKey(Project, on_delete=models.CASCADE)
submission_link = models.URLField()
status = models.CharField(
verbose_name=_("Project Review Status"),
choices=STATUS.choices,
default=STATUS.NOT_SUBMITTED,
max_length=20)
date_submitted = models.DateTimeField(default=timezone.now)
student_comments = models.TextField(blank=True, null=True, default="")
mentor_comments = models.TextField(blank=True, null=True, default="")
def __str__(self):
return f"{self.student.first_name} {self.student.last_name}
{self.project.project_title} submission."
我想了解我做错了什么,因为我已经用尽了所有让它发挥作用的可能性。 Django 版本:4.1.13 django-rest-framework:3.14.0
尝试从属于提交 URL 的视图中删除“permission_classes”。如果它有效,那么原因是应用程序希望经过身份验证的用户访问端点。