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

  1. JS file stored in static/js/
  2. Loaded using {% static %}
  3. Button calls JS function
  4. 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

  1. Change button text dynamically
  2. Show current date using JS
  3. Create multiple buttons
  4. 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

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

Leave a Reply

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