简体   繁体   中英

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.

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

I'm not aware of the NotFound Exception. Is it something you've written yourself or perhaps you meant to use a similar sound Django exception?

https://docs.djangoproject.com/en/2.0/ref/exceptions/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM