In a Django application, I have some checks in a form which will return error = "something".
The thing is that that error will not be defined unless there is any error.
mycharacters = Character.objects.filter(username_id=request.user.id)
if(mycharacters.count() >= 5):
error = True
if not error:
#save to DB
The problem is that if there is no error, error variable will not exist.
I have also thought about a possibility in order to avoid this error, which would be:
error = None
#checks here
if error == None:
#save to DB
But I am not sure whether this would be best approach.
Is there any way to do if error var does not exist:
in Python?
You can do following:
error = mycharacters.count() >= 5
if not error:
...
UPDATE
error = mycharacters.count() >= 5
if error:
to_json = {"incorrect":"Excedeed maximum"}
else:
# Save to DB
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments