Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/django/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python Djangorest框架使用GET和POST创建相同的通用视图_Python_Django_Url_Django Rest Framework_Httpverbs - Fatal编程技术网

Python Djangorest框架使用GET和POST创建相同的通用视图

Python Djangorest框架使用GET和POST创建相同的通用视图,python,django,url,django-rest-framework,httpverbs,Python,Django,Url,Django Rest Framework,Httpverbs,我正在使用通用CreateAPIView在数据库中保存模型。这是我的密码: class AppointmentCreateAPIView(generics.CreateAPIView): permission_classes = (AppointmentCreatePermission,) queryset = Appointment.objects.all() serializer_class = AppointmentSerializer 在我的url.py文件中,

我正在使用通用CreateAPIView在数据库中保存模型。这是我的密码:

class AppointmentCreateAPIView(generics.CreateAPIView):
    permission_classes = (AppointmentCreatePermission,)
    queryset = Appointment.objects.all()
    serializer_class = AppointmentSerializer
在我的
url.py
文件中,我有:

urlpatterns[
    url(r'^appointments/create', AppointmentCreateAPIView.as_view()),
]

此url显然支持POST操作。但是,我希望使用相同的url来处理GET请求,该请求将获取填充约会创建表单所需的数据。我知道我可以为get和post使用单独的url,但这不是我想要的。我是否可能保留相同的url,但使用不同的HTTP谓词,视图将能够处理GET和POST请求?

您可以通过手动将
GET
方法添加到视图中来实现这一点,它看起来是这样的。下面的代码可能不起作用,但会给您一个大致的想法

from rest_framework.response import Response

class AppointmentCreateAPIView(generics.CreateAPIView):
    permission_classes = (AppointmentCreatePermission,)
    queryset = Appointment.objects.all()
    serializer_class = AppointmentSerializer

    def get(self, request, *args, **kwargs):
        serializer = AppointmentSerializer({your_data})
        return Response(serializer.data)

使用
get
method时是否出现错误我准备更改逻辑,但我想保持url不变。是否可以使用相同的url调用两个不同的视图,但使用不同的http谓词?我认为这是不可能的。不同的url,但相同的功能是可能的,但在您的情况下,让方法完全脱离设计。