0
votes

I want to set a variable: correct_captcha, in if statement and return it from the function to HTML, the views is as below:

    def list(request):
        correct_captcha = None
        if request.method == 'POST':
            file = request.FILES.get('file', False)
            ca_mode = request.POST.get('mode', 'word').lower()
            assert ca_mode in ['number', 'word', 'four_number']
            captcha = request.POST.get('captcha')
            ca = Captcha(request)
            if ca.validate(captcha):
                if 'file' in request.FILES:
                    fs = FileSystemStorage()
                    fs.save('(' + datetime.now().strftime('%Y-%m-%d-%H-%M-%S') + 
                            ')' + file.name, file)
                    filesname= str('(' + datetime.now().strftime('%Y-%m-%d-%H-
                            %M-%S') + ')' + file.name)
                else:
                    filesname = ''
                add_obj = enquiry(file=filesname)
                add_obj.save()
                correct_captcha = 0
                return correct_captcha
            else:
                correct_captcha = 1
                return correct_captcha
        return render(request, 'list.html', {'correct_captcha':correct_captcha})

But it did not work, how can I do to return this variable in function?

2
Can you please say more about why "it did not work"?Dane Hillard
put a pdb before ` return render(request, 'list.html', {'correct_captcha':correct_captcha})` and check value of correct_captcha.Vipin Mohan
I want to use correct_captcha as 1 or 0 instead of None, but it is always None in HTML @DaneHillardyuchen huang
You only seem to set correct_captcha if ca.validate(captcha) returns something truthy. Are you sure that is happening?Dane Hillard
the if ca.validate(captcha) statement is okey, i am sure. I think maybe because I lost a return in the if request.method == 'POST' statement. I added a return in the if request.method == 'POST', but still don't set the variable..yuchen huang

2 Answers

1
votes
def list(request):
        correct_captcha = None
        if request.method == 'POST':
            file = request.FILES.get('file', False)
            ca_mode = request.POST.get('mode', 'word').lower()
            assert ca_mode in ['number', 'word', 'four_number']
            captcha = request.POST.get('captcha')
            ca = Captcha(request)
            if ca.validate(captcha):
                if 'file' in request.FILES:
                    fs = FileSystemStorage()
                    fs.save('(' + datetime.now().strftime('%Y-%m-%d-%H-%M-%S') + 
                            ')' + file.name, file)
                    filesname= str('(' + datetime.now().strftime('%Y-%m-%d-%H-
                            %M-%S') + ')' + file.name)
                else:
                    filesname = ''
                add_obj = enquiry(file=filesname)
                add_obj.save()
                correct_captcha = 0
                return render(request, 'list.html', {'correct_captcha':correct_captcha})
            else:
                correct_captcha = 1
                return render(request, 'list.html', {'correct_captcha':correct_captcha})
        return render(request, 'list.html')

in django if you are trying to send some variable to the template you cannot do return, for that you need to send it as a dictionary context , so try the above code in the view

1
votes

I think it is because of your return statement. you do not need to have it in the if else part.

The return statement causes your function to exit and hand back a value to its caller. The return statement is used when a function is ready to return a value to its caller.

Please have a look at here

Change your code as below (we need to remove the return correct_captcha)

def list(request):
    correct_captcha = None
    if request.method == 'POST':
        file = request.FILES.get('file', False)
        ca_mode = request.POST.get('mode', 'word').lower()
        assert ca_mode in ['number', 'word', 'four_number']
        captcha = request.POST.get('captcha')
        ca = Captcha(request)
        if ca.validate(captcha):
            if 'file' in request.FILES:
                fs = FileSystemStorage()
                fs.save('(' + datetime.now().strftime('%Y-%m-%d-%H-%M-%S') + 
                        ')' + file.name, file)
                filesname= str('(' + datetime.now().strftime('%Y-%m-%d-%H-
                        %M-%S') + ')' + file.name)
            else:
                filesname = ''
            add_obj = enquiry(file=filesname)
            add_obj.save()
            correct_captcha = 0
        else:
            correct_captcha = 1

        # edit: return moved inside the if condition
        # avoids local variable referenced before assignment error
        return render(request, 'list.html', {'correct_captcha':correct_captcha})
    return render(request, 'list.html')