I want my code to display a custom error message that depends on the type of error it encounters.
from .forms import NameForm, IdForm
from django.shortcuts import render
from django.http import HttpResponse, HttpResponseRedirect
from django.contrib import messages
from client_storage import insert
import mysql.connector.errors
from mysql.connector.errors import Error
import MySQLdb
def sign_in(request):
#we need to handle all the data that was just typed, we'll add a condition for that
if request.method == "POST":
#here will construct the form with the POST data
form = NameForm(request.POST)
#the next part is to check that the information submitted is valid
if form.is_valid():
post = form.save()
post.save()
return HttpResponse(post.question_text)
else:
return HttpResponse("Form is invalid")
else:
form = NameForm()
return render(request, 'checkin/base.html', {'form': form})
#this view will be the sign-up view, where new clients will be given new ID numbers for training
def sign_up(request):
if request.method == "POST":
form = IdForm(request.POST)
if form.is_valid():
post = form.save()
post.save()
ID = post.id_text
#we'll call an external function that checks membership of the users input in the database
# query is the first element of the tuple that is returned from insert()
query = insert(post.id_text)
if query == 1062:
messages.add_message(request, messages.INFO, 'Already taken ')
return HttpResponseRedirect('sign_up')
if query == 1054:
messages.add_message(request, messages.INFO, 'Invalid input')
return HttpResponseRedirect('sign_up')
else:
messages.add_message(request, messages.INFO, 'Thank you for signing up!')
return HttpResponseRedirect('sign_up')
# if the user enters a number that is already in use raise an 'duplicate' error
# Capture the exception here
else:
return HttpResponse('That text is invalid')
else:
form = IdForm()
return render(request, 'checkin/base.html', {'form': form})
For the `except` block I'm trying to figure out how to display either "Already taken" or "Invalid input" depending on the error code. However only "Already taken" ever appears. I feel like the problem is that the exception is being thrown before it even gets to the `if` clauses?
I'm using another file for the INSERT
process:
import MySQLdb
import mysql.connector
from mysql.connector import errorcode
from django.contrib import messages
#Use a function to insert new information into the database
def insert(info):
#open a connection
db = MySQLdb.connect('localhost','root','password', 'CLIENTS')
#prepare a cursor
cursor = db.cursor()
#prepare SQL query
sql = "INSERT INTO clients(ID) VALUES (%s)" % info
try:
#execute the command
cursor.execute(sql)
#commit changes to the database
print 'success'
db.commit()
except MySQLdb.Error as e:
#return the first element in the tuple that contains the error message, which will be the errorcode
if e[0] == 1062:
return e[0]
if e[0] == 1054:
return e[0]
#disconnect from server
db.close()
EDIT The problem seems to have been that I was using mysql.connector.error
instead of MySQLdb.Error
.The mysql
website seems to only use the former. And there isn't a lot of official documentation on the latter it seems, but thankfully I found this site. I changed the code so the sign_in
view would recieve the returned
info from the external insert
fuction then act accordingly.