javascript - 如何在 django 框架中加载静态 JavaScript 文件
问题描述
我知道我做错了,在查阅了文档并观看了 3 个 YouTube 视频后,这些视频在前 7 分钟内旋转并复制它们仍然无法正常工作
我基本上是想通过点击一个按钮来打个招呼
我的 HTML 中的相关行
{% extends 'box2.html' %}
{% load static %}
{% block content %}
<button class="btn btn-success mt-3" type="button" onclick="handle_do_thing_click()" type="">Add Planner</button>
{% endblock %}
{% block js_block %}
<script type = "text/javascript/" src="{% static 'js/doThing.js' %}"></script>
{% endblock %}
我的 settings.py 静态设置
INSTALLED_APPS = ['django.contrib.staticfiles']
STATIC_URL = '/static/'
STATICFILES_DIRS = [BASE_DIR / 'static']
我的 url.py 文件
from django.urls import path
from django.contrib.auth import views as auth_views
from django.conf.urls.static import static
from django.conf import settings
urlpatterns = [ these work
] + static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
我存在于 app/static/js/doThing.js 中的 js 文件是 hello world
function handle_do_thing_click() {
console.log("hello world");
}
控制台错误是 Uncaught ReferenceError: handle_do_thing_click is not defined at HTMLButtonElement.onclick
解决方案
对我来说,type="text/javascript/"
从<script>
标签中删除使它起作用。
推荐阅读
- python-3.x - 将多列映射到熊猫中的新列
- r - invalidateLater 用于递增值
- typescript - 使用 Typescript 为 Vue 项目设置单元测试失败并出现 TypeError:无法解构“未定义”或“空”的属性“polyfills”
- google-apps-script - Google Apps Script : Approval workflow with html, google sheet, pdf
- three.js - Theejs - Change texture on collada element with uses ShaderMaterial
- python - GDAL Intersection return value
- jquery - jquery loading gif loads after ajax call
- ios - 安装配置文件时出现问题
- excel - Download .csv from an indirect Asana link
- excel - Pulling multiple names that are listed from a different sheet to another