๐ Introduction
This program extends the previous concept of URL parameters.
Instead of square, we will now calculate the cube of a number.
๐ Cube = number ร number ร number
๐ฏ Program Statement
๐ Accept a number as URL parameter and display its cube.
โ๏ธ Step 1: Create Project and App
django-admin startproject myproject
cd myproject
python manage.py startapp myapp
โ๏ธ Step 2: Register App
๐ File: settings.py
๐น Path:
myproject/myproject/settings.py
๐น Code:
INSTALLED_APPS = [
...
'myapp',
]
โ๏ธ Step 3: Configure Templates
๐ File: settings.py
๐น Path:
myproject/myproject/settings.py
๐น Code:
import os
TEMPLATES = [
{
'DIRS': [os.path.join(BASE_DIR, 'templates')],
'APP_DIRS': True,
},
]
โ๏ธ Step 4: Create View
๐ File: views.py
๐น Path:
myproject/myapp/views.py
๐น Code:
from django.shortcuts import render
def cube(request, num):
result = num * num * num
return render(request, 'cube.html', {
'number': num,
'cube': result
})
๐ง Explanation
numcomes from URL- Cube calculated as
num * num * num - Result passed to template
โ๏ธ Step 5: URL Mapping
๐ File: urls.py
๐น Path:
myproject/myproject/urls.py
๐น Code:
from django.contrib import admin
from django.urls import path
from myapp import views
urlpatterns = [
path('admin/', admin.site.urls),
path('cube/<int:num>/', views.cube),
]
๐ง Explanation
<int:num>
- Captures integer from URL
- Sends it to view
โ๏ธ Step 6: Create Template
๐ File: cube.html
๐น Path:
myproject/templates/cube.html
๐น Code:
<!DOCTYPE html>
<html>
<head>
<title>Cube</title>
</head>
<body>
<h1>Cube Calculation</h1>
<p>Number = {{ number }}</p>
<p>Cube = {{ cube }}</p>
</body>
</html>
โ๏ธ Step 7: Run Server
python manage.py runserver
๐ Step 8: Output
Open:
๐ http://127.0.0.1:8000/cube/3/
โ Output:
Number = 3
Cube = 27
๐ง How It Works
- User enters
/cube/3/ - Django extracts
3 - View calculates cube
- Data passed to template
- Template displays result
โ ๏ธ Common Errors
โ URL not working
๐ Check:
path('cube/<int:num>/', views.cube)
โ Wrong output
๐ Check calculation:
num * num * num
โ Template not found
๐ Ensure:
templates/cube.html
๐งช Practice Questions
- Display square and cube together
- Accept two numbers and display cube of both
- Display result in table
- Use negative numbers
๐ค Viva Questions & Answers
1. What is cube of a number?
Cube is obtained by multiplying a number three times by itself. Example: 3ยณ = 27.
2. How is cube calculated in Python?
Cube is calculated using num * num * num or num ** 3. Both give the same result.
3. What is URL parameter in this program?
The number passed in URL is the parameter. It is received by the view function.
4. Can we use power operator instead?
Yes, we can use num ** 3 to calculate cube. It is a shorter and cleaner method.
5. What is the difference between square and cube program?
Square multiplies number twice, cube multiplies it three times. Logic remains similar.
6. Can we combine square and cube in one program?
Yes, we can calculate both and pass them to template. It improves efficiency.
7. What is dynamic URL?
A URL that changes based on input values. It allows flexible routing.
8. What is path converter?
It defines type of input in URL. Example: <int:num> ensures integer value.
9. What happens if we pass string instead of number?
Django will not match the URL and show a 404 error. Type must match converter.
10. Why use templates instead of HttpResponse?
Templates provide better UI and separation of logic. They allow structured HTML display.
๐ Navigation
๐ Next Post: Accept String Parameter and Display Greeting
๐ Back to List: Django Programs (60 Questions with Solutions)
Further Reading
Introduction to Django Framework and its Features
Examples of Array Functions in PHP
Registration Form Using PDO in PHP
Inserting Information from Multiple CheckBox Selection in a Database Table in PHP
- Angular
- ASP.NET
- C
- C#
- C++
- CSS
- Dot Net Framework
- HTML
- IoT
- Java
- JavaScript
- Kotlin
- PHP
- Power Bi
- Python
- Scratch 3.0
- TypeScript
- VB.NET
