Advertisement
# A view function with try-except block
def getdata(request):
a,b= 10,0
# If a is divided by b(which has a value 0);
c=a/b
return HttpResonse(c)
from django.urls import path
from . import views
urlpatterns = [
path('form_submit/', views.getdata),
]
Advertisement
django-admin startproject django_exception
django_exception/
manage.py
django_exception/
__init__.py
settings.py
urls.py
wsgi.py
python manage.py runserver
E:\Django Projects\django_exception>python manage.py runserver
Watching for file changes with StatReloader
Performing system checks...
System check identified no issues (0 silenced).
You have 16 unapplied migration(s). Your project may not work properly until you
apply the migrations for app(s): admin, auth, contenttypes.
Run 'python manage.py migrate' to apply them.
September 19, 2019 - 17:37:35
Django version 2.2.1, using settings 'django_exception.settings'
Starting development server at http://127.0.0.1:8000/
Quit the server with CTRL-BREAK.
python manage.py startapp exception_handling
exception_handling/
__init__.py
admin.py
apps.py
models.py
tests.py
views.py
migrations/
__init__.py
<html>
<head><title>Django - Handling Exceptions - Decodejava.com</title></head>
<body>
Please enter two digits:
<form action="form_submit/" method="post">
{% csrf_token %}
{{ form.as_p }}
<input type="submit" value="Get Result">
</form>
</body>
</html>
from django import forms
class NameForm(forms.Form):
first_number = forms.IntegerField(label='First Number ')
second_number = forms.IntegerField(label='Second Number ')
from django.shortcuts import render
from django.http import HttpResponse
from .forms import NameForm
def get_form(request):
# if the form is requested for the first time to display it to the user
form = NameForm()
return render(request, 'template_ex.htm', {'form': form})
def get_result(request):
# if this is a POST request we need to process the form data
if request.method == 'POST':
# create a form instance and populate it with data from the request:
form = NameForm(request.POST)
# check whether the form data is valid
if form.is_valid():
try:
# Getting the form data
print(form.cleaned_data.get("first_number"))
first_num = form.cleaned_data.get("first_number")
second_num= form.cleaned_data.get("second_number")
c = first_num/second_num
html = "<html>&llt;body>The result of dividing %s with %s is %s.</body></html>" % (first_num, second_num, c)
return HttpResponse(html)
# A matching type of exception is declared in except block
except ZeroDivisionError:
# A message related to caught exception is printed in except block
return HttpResponse('A ZeroDivisionError exception is caught')
from django.urls import path
from . import views
urlpatterns = [
path('', views.get_form),
path('form_submit/', views.get_result),
]
from django.contrib import admin
from django.urls import include, path
urlpatterns = [
path('welcome/template/', include('exception_handling.urls')),
]
'DIRS': ['E:/Django Projects/django_exception/exception_handling'],
Advertisement
Advertisement
Please check our latest addition
C#, PYTHON and DJANGO
Advertisement