
In Django, conditional statement is used to check for a condition, if the condition is true it execute a block of statements and if the condition is false then it execute another block of statements. This all done with the help of if-elif-else statement.
Example 1: Find number is odd or even.
urls.py
path('odd-even', views.odd-even, name="odd-even")
views.py
def odd-even(request):
c='';
if request.method=="POST":
n=eval(request.POST.get('num1'))
if n%2==0:
c='Even'
else
c='Odd'
return render(request, 'odd-even.html', {'c':c})
odd-even.html
<form method="post">
{% csrf_token %}
Enter Number <input type="text" name="num1">
<button>Submit</button>
<p>Output: {{c}}</p>
</form>
Exercise 1: Create simple marksheet.
Ad: