Django

Accept Number as URL Parameter and Display its Cube

๐Ÿ“Œ 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

  • num comes 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

  1. User enters /cube/3/
  2. Django extracts 3
  3. View calculates cube
  4. Data passed to template
  5. 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

  1. Display square and cube together
  2. Accept two numbers and display cube of both
  3. Display result in table
  4. 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

Django Practice Exercise

Examples of Array Functions in PHP

Basic Programs in PHP

Registration Form Using PDO in PHP

Inserting Information from Multiple CheckBox Selection in a Database Table in PHP

programmingempire

princites.com

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *