Django

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

You may also like...

Leave a Reply

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