Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/amazon-s3/2.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 确保TemplateView';中的代码逻辑的常见技术是什么;获取一次执行的上下文数据?_Python_Django - Fatal编程技术网

Python 确保TemplateView';中的代码逻辑的常见技术是什么;获取一次执行的上下文数据?

Python 确保TemplateView';中的代码逻辑的常见技术是什么;获取一次执行的上下文数据?,python,django,Python,Django,目前,我有以下付款按钮 <a href="https://localhost/subscribe/?payload_nonce=token123">PAY NOW</a> 我想知道,确保TemplateView的get_context_数据中的代码逻辑执行一次的常用技术是什么?这里真正的问题是 正确的解决方案是使用一个只接受POST请求的专用视图(这意味着您需要一个HTML表单而不是一个链接),该视图将处理步骤1和2以及。当然,您必须将结果(以及相关的令牌)存储在某个地

目前,我有以下付款按钮

<a href="https://localhost/subscribe/?payload_nonce=token123">PAY NOW</a>

我想知道,确保TemplateView的get_context_数据中的代码逻辑执行一次的常用技术是什么?

这里真正的问题是

正确的解决方案是使用一个只接受POST请求的专用视图(这意味着您需要一个HTML表单而不是一个链接),该视图将处理步骤1和2以及。当然,您必须将结果(以及相关的令牌)存储在某个地方,以便您可以1。避免为同一令牌和2重新发送两次付款。在模板视图的
get\u context\u data
方法中检索与令牌关联的结果


注意:当然,您也可以在同一个视图中处理GET和POST请求,但是
TemplateView
可能不是最佳选择(实际上,除非您需要继承,否则基于类的视图很少是最佳选择-基于函数的视图通常要简单得多)

多亏了布鲁诺·德舒利尔。这就是如何进行代码重构

from django.views.generic import TemplateView
from django.views.generic import View

class SubscribeView(View):
    def post(self, request):
        # Step 1: Get token input from user
        #
        payload_nonce = self.request.POST.get("payload_nonce")


        # Step 2: Payment gateway processes the 
        #         received token, and return success/fail result.
        ...
        ...
        ##############################
        # Submit it to payment gateway
        ##############################
        ...
        ...

        # Redirect to SubscribeDoneView, for page rendering purpose.
        return redirect(reverse('subscribe_done') + query_string)


class SubscribeDoneView(TemplateView):
    template_name = 'subscribe_done.html'

    def get_context_data(self, **kwargs):
        # Call the base implementation first to get a context
        context = super(SubscribeDoneView, self).get_context_data(**kwargs)

        # Step 3: Display success/fail result to user.
        #
        is_success = (self.request.GET.get('is_success') == 'True')
        message = self.request.GET.get('message')

        context['is_success'] = is_success
        if is_success is False and message is not None:
            context['message'] = message

        return context
from django.views.generic import TemplateView
from django.views.generic import View

class SubscribeView(View):
    def post(self, request):
        # Step 1: Get token input from user
        #
        payload_nonce = self.request.POST.get("payload_nonce")


        # Step 2: Payment gateway processes the 
        #         received token, and return success/fail result.
        ...
        ...
        ##############################
        # Submit it to payment gateway
        ##############################
        ...
        ...

        # Redirect to SubscribeDoneView, for page rendering purpose.
        return redirect(reverse('subscribe_done') + query_string)


class SubscribeDoneView(TemplateView):
    template_name = 'subscribe_done.html'

    def get_context_data(self, **kwargs):
        # Call the base implementation first to get a context
        context = super(SubscribeDoneView, self).get_context_data(**kwargs)

        # Step 3: Display success/fail result to user.
        #
        is_success = (self.request.GET.get('is_success') == 'True')
        message = self.request.GET.get('message')

        context['is_success'] = is_success
        if is_success is False and message is not None:
            context['message'] = message

        return context