Add JavaScript File through Static Folder and Demonstrate Button Action
π 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
