Tengo una plantilla base que sirve como estructura común para las plantillas secundarias. Dentro de esta plantilla base, tengo la biblioteca jQuery incluida justo antes de la etiqueta del body de cierre.
<!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> . . </head> <body> {% block content %} {% endblock %} <script src="path/to/jQuery/library"></script> </body> </html>Ahora mi plantilla secundaria tiene el siguiente código
{% extends 'base_template.html' %} {% block content %} <h1>How you doing?</h1> <script type="text/javascript"> // this code is specific to this template, may not be used in another template // but can't use jQuery here since the library hasn't loaded yet </script> {% endblock %} Entonces, ¿cómo haría para ejecutar el código javascript dentro child template solo después de que la plantilla base haya terminado de cargar la biblioteca jQuery?
Necesitas usar 2 bloques:
<!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> . . </head> <body> {% block content %} {% endblock %} <script src="path/to/jQuery/library"></script> {% block scripts %} {% endblock %} </body> </html> {% extends 'base_template.html' %} {% block content %} <h1>How you doing?</h1> {% endblock %} {% block scripts %} <script type="text/javascript"> // this code can "use" jquery </script> {% endblock %}