I am trying to connect the javascript page to the HTML template. I tried many ways but It doesn't show anything. There are no errors but Js file is not responding.
The index.html file is:
{% extends "network/layout.html" %}
{% load static %}
{% block body %}
<!-- <form action="" method="">
<input type="text" name="post" id="post-body">
<input type="submit" name="" id="postbtn" value="Post">
</form> -->
<input type="submit" name="btn" id="btn" value="check">
{% endblock %}
{% block script %}
<script src="{% static 'network/post.js' %}" type="text/javascript"></script>
{% endblock %}
the post.js file is:
document.addEventListener('DOMContentLoaded', function(){
document.querySelector('#btn').onclick = function(){
document.querySelector('#heading').innerHTML = 'hello';
};
})
alert("Hello, Welcome to Javatpoint");
the settings.py :
# Internationalization
# https://docs.djangoproject.com/en/3.0/topics/i18n/
LANGUAGE_CODE = 'en-us'
TIME_ZONE = 'UTC'
USE_I18N = True
USE_L10N = True
USE_TZ = True
# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/3.0/howto/static-files/
STATIC_URL = '/static/'
and urls.py file:
from django.urls import path
from .views import *
from . import views
urlpatterns = [
path("", views.index, name="index"),
path("login", views.login_view, name="login"),
path("logout", views.logout_view, name="logout"),
path("register", views.register, name="register"),
path('posts', compose_post, name='post' )
]
Please help me with this. I will be very thankful to you.
Just include this in your layout.html
{% block script %}
{% endblock script %}