[Django]-How to check if an object exists in the database and then insert it into a form in django

1👍

You also have to catch the DoesNotExist error. Change the if query to a try except block:

try:
    result = CertificateHolder.objects.get(cert_no=query)
except DoesNotExist:
    pass

3👍

You have to use the Django validation and try to access the database from the ‘clean’ method the way @ilse2005 referred to . if you got DoesNotExist exception you have to raise an error in the form itself and handle it in your template.

Leave a comment