Estoy tratando de conectar la página de JavaScript a la plantilla HTML. Intenté de muchas maneras pero no muestra nada. No hay errores, pero el archivo Js no responde.
El archivo index.html es:
{% 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 %}el archivo post.js es:
document.addEventListener('DOMContentLoaded', function(){ document.querySelector('#btn').onclick = function(){ document.querySelector('#heading').innerHTML = 'hello'; }; }) alert("Hello, Welcome to Javatpoint");la configuración.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/'y archivo urls.py:
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' ) ]Por favor, ayúdame con esto. Te estaré muy agradecido.
Solo incluye esto en tu diseño.html
{% block script %} {% endblock script %}