在Django中,来自js静态文件的事件监听器指向了错误的路径。

问题描述 投票:0回答:1

这是我第一次尝试在Django中整合前端和后端。目前我的文件结构如下。

my_project
 | 
 +-- my_app
 |  |  
 |  +-- ...
 |  +-- static
 |  |  |
 |  |  +--my_app
 |  |  |  +--app.js
 |  |  |  +--style.css
 |  |  |  +--back.png
 |  |  |  +--dice-1.png
 |  |  |  +--dice-2.png
 |  |  |  +--dice-3.png
 |  |  |  +--dice-4.png
 |  |  |  +--dice-5.png
 |  |  |  +--dice-6.png
 |  +-- templates
 |  |  |
 |  |  +--my_app
 |  |  |  +--index.html
 |  |  
 +-- manage.py

问题是我的css文件从my_appstatic加载得很好 包括页面上的初始图片文件 但当我尝试在app.js文件上做一个点击事件来加载静态文件夹中的图片时 控制台收到一个错误信息

GET http://localhost:8000/js_dom/dice-1.png 404 (Not Found)

从我的js文件。

    // setter: change dice img
    var diceDOM = document.querySelector('.dice');  // select dice DOM
    diceDOM.src = `dice-${dice}.png`;  // use correct png based on roll <-- this is where I am getting the error
});

初始图像是从我的html文件加载的。

<img src="{% static 'js_dom/dice-5.png' %}" alt="Dice" class="dice"> 

+-- my_app

http://localhost:8000/static/js_dom/dice-5.png

这和我从控制台得到的错误信息很不一样 Which is very different from what I was getting from the console that threw the error.

我确保从settings.py加载了所有的静态文件。

STATIC_URL = '/static/'

STATICFILES_DIRS = (
    os.path.join(BASE_DIR, 'static'),
)

这是项目的urls.py:

from django.contrib import admin
from django.urls import path, include


urlpatterns = [
    path('admin/', admin.site.urls),
    path('js_dom/', include('js_dom.urls'))
]

这是应用程序的urls.py:

from django.urls import path
from . import views

urlpatterns = [
    path('', views.js_dom)
] 

有没有办法配置点击指向哪里?(用staticjs_dom代替js_dom)

javascript django dom
1个回答
© www.soinside.com 2019 - 2024. All rights reserved.