Django运行从父进程导入的javascript函数,由子进程调用

时间:2015-03-05 16:28:34

标签: javascript django inheritance

我在静态的外部文件夹中有一个javascript文件。我的index.html没有运行从base.html导入的javascript函数。

文件路径:

-- project
   -- app
      -- templates
         -- app name
            -- index.html
   -- src
   -- static
      -- hello_world.js
   -- templates
      -- base.html

从浏览器查看页面来源

<!DOCTYPE HTML> 
<html>

  <head>
    <style>
      body {
        margin: 0px;
        padding: 0px;
      }
    </style>

    <script type='text/javascript' src="/static/hello_world.js"></script>
  </head> 

  <body>

<div id="navigation">
<table border=0 cellpadding=7>
<tr>
    <td><a href="/game">Home</a></td>

      <td><a href="/game/registration">Register</a></td>
      <td><a href="/game/login">Login</a></td>

</tr>
</table>
</div>


<div id="content">

    <h2> Welcome to WAM's board 2 player board games! </h2>
    <canvas onload="entryPoint();" id="myCanvas" width="578" height="200"></canvas>

</div>
</body>
</html>

base.html文件

{% block doctype %}<!DOCTYPE HTML> {% endblock %}
<html>
{% block head %}
  <head>
    <style>
      body {
        margin: 0px;
        padding: 0px;
      }
    </style>
{% load staticfiles %}
    <script type='text/javascript' src="{% static "hello_world.js" %}"></script>
  </head> 
{% endblock %}
  <body>
{% block navbar %}
<div id="navigation">
<table border=0 cellpadding=7>
<tr>
    <td><a href="/game">Home</a></td>
{% if user_logged_in %}
      <td><a href="/game/upload">Upload</a></td>
      <td><a href="/game/challenge_user">Challenge User</a></td>
      <td><a href="/game/logout">Logout</a> of {{ user_name }}</td>
{% else %}
      <td><a href="/game/registration">Register</a></td>
      <td><a href="/game/login">Login</a></td>
{% endif %}
</tr>
</table>
</div>

{% endblock %}
<div id="content">
{% block content %}{% endblock %}
</div>
</body>
</html>

的index.html

{% extends "base.html" %}
{% block navbar %}{{ block.super }}{% endblock %}
{% block content %}
    <h2> Welcome to WAM's board 2 player board games! </h2>
    <canvas onload="entryPoint();" id="myCanvas" width="578" height="200"></canvas>
{% endblock %}

1 个答案:

答案 0 :(得分:2)

把这个

import os

BASE_DIR = os.path.dirname(os.path.dirname(__file__)) # already present in newest django versions
STATICFILES_DIRS = (
    os.path.join(BASE_DIR, 'static'),
)
在您的settings.py中

,或者只是将静态文件放在app文件夹中的静态文件夹中