0

In my code, there is an if condition, which should be passed but is skipping for some reason. It looks like this:

 if request.method == 'POST':
        email = request.POST.get('email',None) #<-- abc@domain.com
        user = User.objects.get(username = request.user.username) 
        user_email = user.email                #<-- u''
        if user_email is None or '' and email is not None:
                user.email = email

The second if condition is skipping even though it's correct(user_email is empty string and email is not None). What am I doing wrong? I also tried splitting condition in two steps but it is still skipping.

Maxsteel
  • 1,922
  • 4
  • 30
  • 55

2 Answers2

1

The problem is

if user_email is None or ''

You mean

if user_email in [None, '']

You cannot include multiple values with or after is, or anywhere else, actually, English and Python (and pretty much every other programming language) are different in this regard. Your code basically evaluates to

if (user_email is None) or ('' == True)

which of course doesn't make much sense.

tripleee
  • 175,061
  • 34
  • 275
  • 318
0

Try this.

 if user_email is None or user_email is '' and email is not None:
            user.email = email

I think you might not be getting the if working correctly because you need to repeat user_mail is '' after the or.

Orkem
  • 91
  • 5