如何在Django中成功处理POST请求

时间:2019-04-28 22:40:42

标签: python django

我是Django和Web开发的新手。我在models.py中有一个课程模型。我本质上是想在前端添加课程。我将使用POST到服务器。我只是对自己做得正确还是缺少什​​么感到困惑。

我已经创建了一个AddCourseForm类,以尝试解决这个问题。

models.py

class Course(models.Model): 
    id = models.AutoField(primary_key=True)
    name = CharField(max_length=100)

class Project(models.Model):
    id = models.AutoField(primary_key=True)
    desc = CharField(max_length=150)
    name = CharField(max_length=50)
    storyboard_file_path = TextField()
    storyboard_completed = BooleanField(default=False)
    filming_complete = BooleanField(default=False)
    audio_complete = BooleanField(default=False)
    production_complete = BooleanField(default=False)
    aggregation_complete = BooleanField(default=False)
    video_file_path = TextField()
    final_review_complete = BooleanField(default=False)
    course = models.ForeignKey(Course, on_delete=models.CASCADE, related_name="projects", null=True, blank=True)

serializer.py

    class Meta:
        model = Project
        fields = ('id', 'desc', 'name', 'storyboard_file_path', 
        'storyboard_completed', 'filming_complete', 'audio_complete',  
        'production_complete', 'aggregation_complete', 'video_file_path',
        'final_review_complete', 'course')

    def create(self, validated_data):
        instance = self.Meta.model(**validated_data)
        instance.save()
        return instance

    def update(self, instance, validated_data):
        for attr, value in validated_data.items():
                setattr(instance, attr, value)
        instance.save()
        return instance

class CouserSerializer(serializers.ModelSerializer):
    projects = ProjectSerializer(many=True, read_only=True)

    class Meta:
        model = Course
        fields = ('id', 'name', 'projects')

    def create(self, validated_data):
        instance = self.Meta.model(**validated_data)
        instance.save()
        return instance

forms.py


    class Meta:
        model = Course
        fields = [
            'name'
        ]

views.py


    queryset = Course.objects.all()
    serializer_class = serializers.CouserSerializer


def add_course(request):

    if request.POST:
        course_form = AddCourseForm(request.POST)

        if course_form.is_valid():
            new_course_form = course_form.save()
            return redirect(new_course_form) #get_absolute_url set on model

        else:
            return render(request,
                'service/add_course.html',
                    {'course_form': course_form}
                    )

    else:
        course_form = AddCourseForm(
                        initial={'name': ' '}
                        )
        return render(request,
                'service/add_course.html',
                {'course_form': course_form}
                )

urls.py

router = routers.DefaultRouter(trailing_slash=False)
router.register(r'users', views.UserViewSet)
router.register(r'courses', views.CourseViewSet)
router.register(r'focal', views.FocalUserViewSet)
#router.register(r'add_course', views.CourseCreateViewSet)

urlpatterns = [
    path(r'api/', include(router.urls)),
    path(r'', views.index, name='index')
]

我觉得我一切都正确,但是我不确定如何测试POST请求。我有一个问题,是否需要以任何方式更新urls.py?我不确定如何轻松测试POST请求。

我将不胜感激。

0 个答案:

没有答案