variable 'error' referenced before assignmentRequest

anon

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?

falsetru

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.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

UnboundLocalError: local variable 'error' referenced before assignment

From Dev

Error: Local variable referenced before assignment

From Dev

Python Bizarre Error - Variable Referenced Before Assignment

From Dev

local variable referenced before assignment django error

From Dev

Solving error: local variable 'counter' referenced before assignment

From Dev

error UnboundLocalError: local variable 'currentpl' referenced before assignment:

From Dev

Cython compilation error - local variable referenced before assignment

From Dev

'Local variable referenced before assignment' error, due to confusion with function name?

From Dev

MySQLdb error local variable referenced before assignment (different than usual)

From Dev

Python Error - UnboundLocalError: local variable referenced before assignment

From Dev

Cython compilation error - local variable referenced before assignment

From Dev

Python Error - UnboundLocalError: local variable referenced before assignment

From Dev

Django form error:local variable 'context' referenced before assignment

From Dev

Local variable referenced before assignement

From Dev

Variable referenced before assignment in this code

From Dev

Local variable '...' referenced before assignment

From Dev

local variable referenced before assignment

From Dev

global variable referenced before assignment

From Dev

"Local variable referenced before assignment" error while declaring variable same as the imported class

From Dev

Variable Referenced before Assignment Error - How to use variables across multiple functions?

From Dev

python2.7 error: UnboundLocalError: local variable 'i' referenced before assignment

From Dev

I keep getting the following error "local variable 'total_results' referenced before assignment"

From Dev

django authentication in signup page error : 'local variable 'user' referenced before assignment

From Dev

Variable Referenced before Assignment Error - How to use variables across multiple functions?

From Dev

ssh-import-id failing with "ERROR local variable 'keys' referenced before assignment"

From Dev

Error local variable 'node' referenced before assignment in my linked list implementation?

From Dev

Local Variable referenced before assignment inside of a class

From Dev

Local variable referenced before assignment in decorated method

From Dev

Changing variable leads to referenced before assignment

Related Related

  1. 1

    UnboundLocalError: local variable 'error' referenced before assignment

  2. 2

    Error: Local variable referenced before assignment

  3. 3

    Python Bizarre Error - Variable Referenced Before Assignment

  4. 4

    local variable referenced before assignment django error

  5. 5

    Solving error: local variable 'counter' referenced before assignment

  6. 6

    error UnboundLocalError: local variable 'currentpl' referenced before assignment:

  7. 7

    Cython compilation error - local variable referenced before assignment

  8. 8

    'Local variable referenced before assignment' error, due to confusion with function name?

  9. 9

    MySQLdb error local variable referenced before assignment (different than usual)

  10. 10

    Python Error - UnboundLocalError: local variable referenced before assignment

  11. 11

    Cython compilation error - local variable referenced before assignment

  12. 12

    Python Error - UnboundLocalError: local variable referenced before assignment

  13. 13

    Django form error:local variable 'context' referenced before assignment

  14. 14

    Local variable referenced before assignement

  15. 15

    Variable referenced before assignment in this code

  16. 16

    Local variable '...' referenced before assignment

  17. 17

    local variable referenced before assignment

  18. 18

    global variable referenced before assignment

  19. 19

    "Local variable referenced before assignment" error while declaring variable same as the imported class

  20. 20

    Variable Referenced before Assignment Error - How to use variables across multiple functions?

  21. 21

    python2.7 error: UnboundLocalError: local variable 'i' referenced before assignment

  22. 22

    I keep getting the following error "local variable 'total_results' referenced before assignment"

  23. 23

    django authentication in signup page error : 'local variable 'user' referenced before assignment

  24. 24

    Variable Referenced before Assignment Error - How to use variables across multiple functions?

  25. 25

    ssh-import-id failing with "ERROR local variable 'keys' referenced before assignment"

  26. 26

    Error local variable 'node' referenced before assignment in my linked list implementation?

  27. 27

    Local Variable referenced before assignment inside of a class

  28. 28

    Local variable referenced before assignment in decorated method

  29. 29

    Changing variable leads to referenced before assignment

HotTag

Archive