๐ Introduction
Django supports JavaScript via static files.
๐ We use JS to:
- Handle button clicks
- Show alerts
- Add interactivity
๐ฏ Program Statement
๐ Add a JavaScript file through static folder and demonstrate button action.
๐ง Concept
๐ Static folder can store:
- CSS
- JS
- Images
โ๏ธ Step 1: Static Folder Structure
๐ Project Structure:
myproject/
static/
js/
script.js
โ๏ธ Step 2: Create JavaScript File
๐ File: script.js
๐น Path:
myproject/static/js/script.js
๐น Code:
function showMessage() {
alert("Hello! Button Clicked Successfully.");
}
โ๏ธ Step 3: Create Template
๐ File: jsdemo.html
๐น Path:
myproject/templates/jsdemo.html
๐น Code:
{% load static %}
<!DOCTYPE html>
<html>
<head>
<title>JavaScript Demo</title>
<!-- Link JS File -->
<script src="{% static 'js/script.js' %}"></script>
</head>
<body>
<h1>JavaScript Integration</h1>
<button onclick="showMessage()">Click Me</button>
</body>
</html>
โ๏ธ Step 4: Create View
๐ File: views.py
๐น Path:
myproject/myapp/views.py
๐น Code:
from django.shortcuts import render
def js_demo(request):
return render(request, 'jsdemo.html')
โ๏ธ 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('js/', views.js_demo),
]
โ๏ธ Step 6: Run Server
python manage.py runserver
๐ Step 7: Output
๐ http://127.0.0.1:8000/js/
โ Output:
- A button appears
- Clicking button โ shows alert message
๐ง How It Works
- JS file stored in
static/js/ - Loaded using
{% static %} - Button calls JS function
- JS executes and shows alert
๐ฅ Key Concepts
Load Static
{% load static %}
Link JS
<script src="{% static 'js/script.js' %}"></script>
Button Action
<button onclick="showMessage()">
JS Function
function showMessage() {
alert("Hello!");
}
โ ๏ธ Common Errors
โ Forgot {% load static %}
๐ JS will not load
โ Wrong path
โ Wrong:
{% static 'script.js' %}
โ Correct:
{% static 'js/script.js' %}
โ Function not found
๐ Ensure:
function showMessage()
matches button:
onclick="showMessage()"
โ JS file not in static folder
๐ Must be inside:
static/js/
๐งช Practice Questions
- Change button text dynamically
- Show current date using JS
- Create multiple buttons
- Validate input using JS
๐ค Viva Questions & Answers
1. What is JavaScript?
JavaScript is a scripting language used to make web pages interactive.
2. How do we add JS in Django?
By placing it in static folder and loading using {% static %}.
3. What is <script> tag?
It is used to include JavaScript in HTML.
4. What is event handling?
Responding to user actions like button clicks.
5. What is onclick?
It is an event that triggers when button is clicked.
6. What is function in JavaScript?
A block of code that performs a task when called.
7. Why use external JS file?
For better organization and reuse.
8. What happens if JS file is missing?
Function will not execute.
9. Can we use multiple JS files?
Yes, multiple files can be included.
10. Why is JS important?
It adds interactivity and improves user experience.
๐ Navigation
๐ Next Post: Bootstrap Styled Responsive Page
๐ 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
