0

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?

yuchen huang
  • 257
  • 1
  • 2
  • 10
  • 3
    Can you please say more about why "it did not work"? – Dane Hillard Jan 18 '18 at 04:41
  • 1
    put a pdb before ` return render(request, 'list.html', {'correct_captcha':correct_captcha})` and check value of correct_captcha. – Vipin Mohan Jan 18 '18 at 04:41
  • I want to use correct_captcha as 1 or 0 instead of None, but it is always None in HTML @DaneHillard – yuchen huang Jan 18 '18 at 04:43
  • You only seem to set `correct_captcha` if `ca.validate(captcha)` returns something truthy. Are you sure that is happening? – Dane Hillard Jan 18 '18 at 04:57
  • 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 Jan 18 '18 at 05:10

2 Answers2

1

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')
Van Peer
  • 2,127
  • 2
  • 25
  • 35
Manishh
  • 1,444
  • 13
  • 23
1
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

Exprator
  • 26,992
  • 6
  • 47
  • 59
  • Thanks, your are correct. It does work. But I have a new question....I want to use this variable in js to alert if the captcha is right. But now this variable's value in js is the last time click, instead of this time...do you have some ideas? – yuchen huang Jan 18 '18 at 05:25
  • if you want to check the value each time the click is trigerred, then you need to do it by ajax, which needs to be called on every click – Exprator Jan 18 '18 at 05:28
  • glad could help you bro :) @yuchenhuang – Exprator Jan 18 '18 at 05:31