2

In Django/Python, how do I catch a specific mySQL error: 'IntegrityError':

try:
   cursor.execute(sql)
except IntegrityError:
    do_something

Not sure what I should import and from where.

user984003
  • 28,050
  • 64
  • 189
  • 285

2 Answers2

8

DId you try from django.db import IntegrityError?

Lorem Ipsum
  • 4,020
  • 4
  • 41
  • 67
Torsten Engelbrecht
  • 13,318
  • 4
  • 46
  • 48
0

Here is a similar response . In summary

from django.db import IntegrityError
from django.shortcuts import render_to_response

try:
# code that produces error
except IntegrityError as e:
return render_to_response("template.html", {"message": e.message})
Community
  • 1
  • 1
user3404455
  • 420
  • 1
  • 3
  • 9