base.html中包含的JavaScript不适用于扩展模板

时间:2017-12-18 14:49:38

标签: javascript django

我有以下项目文件结构:

enter image description here

在base.html中,我导入了一些JS,用于将onclick事件添加到html文件中的按钮:

{% load static %}
<!DOCTYPE html>
<html lang="en">
<head>
    <title>{% block title %}Default Title{% endblock %}</title>
    <link rel="stylesheet" href="{% static 'css/workoutcal.css' %}">
</head>
<body>

{% block userwidget %}
<button type="button" id="logoutbutton">Logout</button>
{% endblock %}

{% block content %}{% endblock %}

{% block footer %}{% endblock %}

<script type="text/javascript" src="{% static "js/base.js" %}"></script>
</body>
</html>

base.js:

logoutbutton = document.getElementById("logoutbutton");
logoutbutton.onclick = logout;

function logout(){
    window.location = "/workoutcal/logout";
}

然后,在calendar.html中:

{% extends "workout/base.html" %}

{% block title %}{{ title }}{% endblock %}

按钮显示在浏览器中,但是当我点击它时,没有任何反应。从浏览器控制台我得到以下错误:

GET http://localhost:8000/static/js/base.js net::ERR_ABORTED
localhost/:359 
GET http://localhost:8000/static/js/base.js 404 (Not Found)

因此显然找不到静态文件。我做错了吗?

编辑:

来自settings.py:

STATIC_URL = '/static/' 

STATIC_ROOTSTATICFILE_DIRS未设置。

0 个答案:

没有答案
相关问题