Django: Catching Integrity Error and showing a customized message using template

前端 未结 6 2117
梦谈多话
梦谈多话 2020-12-13 03:24

In my django powered app there is only one obvious case where \"IntegrityError\" can arise.
So, how can I catch that error and display a message using templates?

6条回答
  •  执笔经年
    2020-12-13 04:09

    Just import IntegrityError

    from django.db import IntegrityError
    

    and use it inside try/Except

    try:
        //Do what is needed to be done
     except IntegrityError as e:
        //return what is supposed to be returned
    

    Thanks @Soren I just Edited my answer

提交回复
热议问题