Django

Accept Number as URL Parameter and Display its Square

๐Ÿ“Œ Introduction

In Django, we can pass values directly through the URL.
These values are called URL parameters.

In this program, we will:

  • Accept a number from URL
  • Calculate its square
  • Display result

๐ŸŽฏ Program Statement

๐Ÿ‘‰ Accept a number as URL parameter and display its square.


โš™๏ธ 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 square(request, num):
result = num * num

return render(request, 'square.html', {
'number': num,
'square': result
})

๐Ÿง  Explanation

  • num comes from URL
  • num * num calculates square
  • Result is passed to template

โš™๏ธ Step 5: URL Mapping (Important ๐Ÿ”ฅ)


๐Ÿ“ 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('square/<int:num>/', views.square),
]

๐Ÿง  Explanation

<int:num>
  • <int:num> โ†’ captures integer from URL
  • num โ†’ passed to view

โš™๏ธ Step 6: Create Template


๐Ÿ“ File: square.html

๐Ÿ”น Path:

myproject/templates/square.html

๐Ÿ”น Code:

<!DOCTYPE html>
<html>
<head>
<title>Square</title>
</head>
<body>

<h1>Square Calculation</h1>

<p>Number = {{ number }}</p>
<p>Square = {{ square }}</p>

</body>
</html>

โš™๏ธ Step 7: Run Server

python manage.py runserver

๐ŸŒ Step 8: Output

Open:

๐Ÿ‘‰ http://127.0.0.1:8000/square/5/


โœ… Output:

Number = 5
Square = 25

๐Ÿง  How It Works

  1. User enters /square/5/
  2. Django extracts 5
  3. Passes it to view as num
  4. View calculates square
  5. Template displays result

โš ๏ธ Common Errors


โŒ URL not working

๐Ÿ‘‰ Check:

path('square/<int:num>/', views.square)

โŒ Type error

๐Ÿ‘‰ Ensure <int:num> is used
(not <str:num>)


โŒ Missing parameter

๐Ÿ‘‰ URL must include number:

/square/5/

๐Ÿงช Practice Questions

  1. Display cube of number
  2. Display square and cube both
  3. Accept float number
  4. Handle negative numbers

๐ŸŽค Viva Questions & Answers


1. What is URL parameter?

URL parameter is a value passed through the URL. It is used to send data to the view dynamically.


2. What is <int:num> in Django?

It is a path converter that captures an integer value from the URL and passes it to the view.


3. Can we use other types in URL parameters?

Yes, Django supports types like str, int, slug, and uuid. These define the type of input.


4. What happens if wrong type is passed?

Django will not match the URL and will show a 404 error. It ensures type safety.


5. How is parameter received in view?

It is received as an argument in the function. Example: def square(request, num).


6. What is dynamic routing?

Dynamic routing allows URLs to change based on input values. It makes applications flexible.


7. Can we pass multiple parameters?

Yes, multiple parameters can be passed. Example: /add/5/10/.


8. What is the use of path converters?

Path converters define the type of variable accepted. They ensure correct data format.


9. What happens if parameter is missing?

Django will not match the URL pattern and show an error. Required parameters must be provided.


10. Why use URL parameters instead of forms?

URL parameters are simple and useful for small data. Forms are better for larger and secure data.


๐Ÿ‘‰ Next Post: Display Cube of Number using URL Parameter
๐Ÿ‘‰ 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 *