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.

2 Answers 2

8

DId you try from django.db import IntegrityError?

Sign up to request clarification or add additional context in comments.

Comments

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.