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?

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

2 Answers 2

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

Sign up to request clarification or add additional context in comments.

3 Comments

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?
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
glad could help you bro :) @yuchenhuang
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')

1 Comment

it shows a error: local variable 'correct_captcha' referenced before assignment

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.