-3

My trouble is the following code

CreateView.py

# Add new function
def error_display(self, message)
   context={
        'message':message,
    }
    return render(self.request, 'error.html', context)

#I modified form_valide function
 def form_valide(self, form)
      .......
      try:
         3<5  #True
      except:
         self.error_dispaly('error1')
      else:
          .......
          if 3<5  #True
              self.error_display('OK')
          return render(self.request, passed.html, {message:'error2'})

Despite 「If」indicate "True", this view render passed.html(displaying 'error2'), but not 'OK' message (「If」seem not to work)

On the other hand,「If」do work with no function (the following code)

if 3<5  #True
   context={msg:'OK'}
   return render(self.request, error.html, context)
return render(self.request, passed.html, {message:'error2'})

This view render error.html(displaying 'OK')

I want to use 「error_display」function

Does anybody tell me Why the function disable 「If」

Django==3.2.8

Genzo Ito
  • 189
  • 11

1 Answers1

0

3<5 will be always True so there won't be any exceptions. Following that, your rest of code will never work.

Also this is the correct usage of if statement:

if (3 < 5):
   context={msg:'OK'}
   return render(self.request, error.html, context)

In any function, you can only return 1 thing. If the thing you'll return depends on a contidion you should use an else statement like this:

if 3<5:
   context={msg:'OK'}
   return render(self.request, error.html, context)
else:
   return render(self.request, passed.html, {message:'error2'})
burak
  • 109
  • 2
  • 8