pythondjangoopenstack-keystone

Why is my try catch solution not working as I expect it


I am trying to validate a form in a django project and part of the validation is to check if a project exists.

The Environment:

python 3.6.3
django 1.10.8
python-keystoneclient 3.14.0

I have this check currently

def clean_projectname(self):
        submitted_data = self.cleaned_data['projectname']
        newproj = "PROJ-FOO" + submitted_data.upper()
        keystone = osauth.connect()
        try:
            project = keystone.projects.find(name=newproj)
            raise forms.ValidationError('The project name is already taken')
        except NotFound:
            return submitted_data

The try section will return either a project object or it will have a 404 not found Exception.

I have tried to except on the NotFound but Django gives me an error

name 'NotFound' is not defined

I would appreciate help with this.


Solution

  • Have you imported NotFound from python-keystoneclient? The only way your code would work is if you had this line somewhere else in your file:

    from keystoneclient.exceptions import NotFound