Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/django/22.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
Django 在单个基于类的视图中处理不同POST请求的最佳方法?_Django_Django Views - Fatal编程技术网

Django 在单个基于类的视图中处理不同POST请求的最佳方法?

Django 在单个基于类的视图中处理不同POST请求的最佳方法?,django,django-views,Django,Django Views,在这里开发我的第一个应用程序,我使用基于类的视图(django.views.generic.base.view)来处理来自网页的请求 在网页上,我有不同的表单发送帖子请求,例如,有文本发布表单、评论表单、投票按钮等。我正在检查POST.has_key(),查看哪个表单已经发布,并根据该表单进行处理 有什么更好的方法?是否可以定义方法名称,如post_text、post_comment等,并配置dispatch()以相应地运行该方法 我会这样做: class AwesomeView(View):

在这里开发我的第一个应用程序,我使用基于类的视图
(django.views.generic.base.view
)来处理来自网页的请求

在网页上,我有不同的表单发送帖子请求,例如,有文本发布表单、评论表单、投票按钮等。我正在检查
POST.has_key()
,查看哪个表单已经发布,并根据该表单进行处理


有什么更好的方法?是否可以定义方法名称,如post_text、post_comment等,并配置dispatch()以相应地运行该方法

我会这样做:

class AwesomeView(View):
    def post(self, request, *args, **kwargs):
        # This code is basically the same as in dispatch
        # only not overriding dispatch ensures the request method check stays in place.

        # Implement something here that works out the name of the 
        # method to call, without the post_ prefix
        # or returns a default method name when key is not found.
        # For example: key = self.request.POST.get('form_name', 'invalid_request')
        # In this example, I expect that value to be in the 'key' variable

        handler = getattr(
                           self,  # Lookup the function in this class
                           "post_{0}".format(key),  # Method name
                           self.post_operation_not_supported  # Error response method
                         )
        return handler(request, *args, **kwargs)

    def post_comment(self, request, *args, **kwargs):
        return HttpResponse("OK")  # Just an example response

我会这样做:

class AwesomeView(View):
    def post(self, request, *args, **kwargs):
        # This code is basically the same as in dispatch
        # only not overriding dispatch ensures the request method check stays in place.

        # Implement something here that works out the name of the 
        # method to call, without the post_ prefix
        # or returns a default method name when key is not found.
        # For example: key = self.request.POST.get('form_name', 'invalid_request')
        # In this example, I expect that value to be in the 'key' variable

        handler = getattr(
                           self,  # Lookup the function in this class
                           "post_{0}".format(key),  # Method name
                           self.post_operation_not_supported  # Error response method
                         )
        return handler(request, *args, **kwargs)

    def post_comment(self, request, *args, **kwargs):
        return HttpResponse("OK")  # Just an example response

为什么不为不同的post操作创建不同的端点(使用视图/方法/类)?为什么不为不同的post操作创建不同的端点(使用视图/方法/类)?方法名称的构造取决于键?例如,如果键有“comment”,那么将调用post_comment?是的,这是正确的(这里是:“post_{0}”。format(key)),如果它不存在,将调用post_operation_not_supported。根据键构造方法名?例如,如果键有“comment”,那么将调用post_comment?是的,这是正确的(这里是:“post_{0}”。format(key)),如果它不存在,将调用post_operation_not_supported。