๐ 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
numcomes from URLnum * numcalculates 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 URLnumโ 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
- User enters
/square/5/ - Django extracts
5 - Passes it to view as
num - View calculates square
- 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
- Display cube of number
- Display square and cube both
- Accept float number
- 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
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
