user984003
user984003

Reputation: 29569

Django / Python: catch MySQL IntegrityError

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.

Upvotes: 2

Views: 3442

Answers (2)

Torsten Engelbrecht
Torsten Engelbrecht

Reputation: 13496

DId you try from django.db import IntegrityError?

Upvotes: 8

user3404455
user3404455

Reputation: 440

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})

Upvotes: 0

Related Questions