how can I return error from a python function and display it in Django temaplate. I have a code base that is similar to the following structure:
In Views.py:
def calculate(num1, num2):
result = int(num1) + int(num2)
return result
def home(request):
if request.method == POST:
user_input_1 = request.POST.get('user_input_1 ')
user_input_2 = request.POST.get('user_input_2 ')
calculator = calculate(user_input_1 , user_input_2 )
context = {
'calculator' : calculator
}
return render(request, 'home.html', context)
return render(request, 'home.html')
So in the case were the user enters a letter instead of a digit, I want to display an error in the django template telling the user about the error. Right now when error occurs the code crashes.
I know that I can write a try and except
to handle the error but I don't know how to display the exact error message on the HTML template. Any Ideas on how to go about this please?
question from:
https://stackoverflow.com/questions/65842295/writing-customized-functions-in-django-views 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…