Home > other >  Local variable 'cid' referenced before assignment
Local variable 'cid' referenced before assignment

Time:10-25

Im trying to add a payment method. I also add Getway and Get Success Message. Now I want to show success message with cid and name in my template page. When I want to get context data, I got This Error. ** Local variable 'cid' referenced before assignment. ** My code:

class CheckoutSuccessView(View):
    model = Transaction
    template_name = 'success.html'

    
    def get(self, request, *args, **kwargs):

        # return render(request, self.template_name,{'transaction':transaction})
        return HttpResponse('nothing to see')

    def post(self, request, *args, **kwargs):

        data = self.request.POST
        
        
        try:
            Transaction.objects.create(
                name = data['value_a'],
                cid = data['value_b'],
                tran_id=data['tran_id'],
                val_id=data['val_id'],
                amount=data['amount'],
                card_type=data['card_type'],
                card_no=data['card_no'],
                 ...
                 ...

            )
            messages.success(request,'Payment Successfull')

            name = data['value_a'],
            cid = data['value_b'],
            
            

            

        except:
            messages.success(request,'Something Went Wrong')

        
        

            
        context = {
                'cid': cid,
                'name' : name
                 }

        return render(request, 'success.html', context)

CodePudding user response:

In case an exception occurred, cid will be undefined. Therefore you need to define it in the except block such as cid = None. Or you can define it before the try block.

     try:
        .......
        cid = data['value_b']
    except:
        .......
        cid = None

Or

    cid = None

    try:
        .......
        cid = data['value_b']
    except:
        .......

CodePudding user response:

You may miss block or put extra indentation. Define it before try block.

You may try this:

    model = Transaction
    template_name = 'success.html'
    
    def get(self, request, *args, **kwargs):

        # return render(request, self.template_name,{'transaction':transaction})
        return HttpResponse('nothing to see')

    def post(self, request, *args, **kwargs):

        data = self.request.POST

        name = data['value_a'],
        cid = data['value_b'],

        try:
            Transaction.objects.create(
                name = data['value_a'],
                cid = data['value_b'],
                tran_id=data['tran_id'],
                val_id=data['val_id'],
                amount=data['amount'],
                card_type=data['card_type'],
                card_no=data['card_no'],
                 ...
                 ...

            )
            messages.success(request,'Payment Successfull')

        except:
            messages.success(request,'Something Went Wrong')

        context = {
                'cid': cid,
                'name' : name
                 }

        return render(request, 'success.html', context)
    ```

* I think you are using sslcommerz-lib for SSLCOMMERZ PAYMENT GATEWAY.*
  • Related