[Fixed]-Django local variable referenced before assignment

1👍

The problem is on this line

sigtracker = sigtracker()

You have imported your model class sigtracker() outside of the view function. In Python, it is not possible to reference this class inside the function, and assign it to the same local variable name.

The quickest fix would be to rename the sigtracker instance to something else, e.g. st:

    st = sigtracker()
    st.ident = form.cleaned_data['ident']
    st.system = form.cleaned_data['system']
    st.name = form.cleaned_data['name']
    st.signiture_type = form.cleaned_data['signiture type']
    st.status = form.cleaned_data['status']
    st.save()
    logger.info("Created new signiture in %s at %s by user %s" % (st.system, request.user))

It is confusing to try to use sigtracker to refer to the model class and model instances. It is recommended to use capitalised names for your models, e.g. SigTracker (this is a pep8 recommendation for Python classes in general).

That way, you can easily tell the difference between the model SigTracker and the model instance sigtracker.

If you rename the model to SigTracker, make sure to update your imports and code everywhere you use it.

Leave a comment