yuchen huang
yuchen huang

Reputation: 257

How to set a variable in if statement and return in function?

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?

Upvotes: 0

Views: 111

Answers (2)

Manishh
Manishh

Reputation: 1484

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')

Upvotes: 1

Exprator
Exprator

Reputation: 27513

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

Upvotes: 1

Related Questions