(Django +外部JavaScript)无法正常工作

时间:2017-04-23 06:07:47

标签: javascript html json django

我有一个使用Django 1.11和Javascript的简单项目无效。当我在HTML中运行js时,json加载和javascripts工作,但是当我将所有这些放入静态文件夹时,javascript加载但没有执行任何操作。我已配置静态文件夹,它加载CSS配置,但不适用于JavaScript。我也没有成功地运行collectstatics。你能帮忙吗?

JS:

(function(){
      'use strict';
      window.onload = function(){ alert("Hi there"));}
      var SentimientosApp = angular.module('SentimientosApp', []);
      SentimientosApp.controller('SentimientosCtrl', function ($scope, $http){
        $http.get("../externalfiles/countries.json").success(function(data) {
          $scope.datos = data;
        });
      });
    }());

网/ sentimientos / index.html中:

{% load static %}
<html ng-app="SentimientosApp">
<head>
    <meta charset="utf-8">
    <title>Angular.js JSON Fetching Example</title>
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.2/css/bootstrap.min.css">
    <link rel="stylesheet" href="{% static 'css/css.css' %}">
    <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/angularjs/1.5.0/angular.min.js"></script>

    <script type="text/javascript" src="{% static 'js/sentimientos.js' %}"></script>

</head>
{% block content %}
<body ng-controller="SentimientosCtrl">
{% block title %}Articles for {{ year }}{% endblock %}
<h2>Angular.js JSON Fetching Example</h2>
<table>
    <tr>
        <th>Code</th>
        <th>Country</th>
        <th>Population</th>
    </tr>
    <tr ng-repeat="d in datos">
        {% verbatim %}
        <td>{{d.code}}</td>
        <td>{{d.name}}</td>
        <td>{{d.population}}</td>
        <td><input type="text" ng-model="new_title"/>{{ new_title }}</td>
        {% endverbatim %}
    </tr>
</table>
</body>
{% endblock %}
</html>

JSON位于静态的文件夹中,具有相同的结构:

http://ladimi.com/bigdata/externalfiles/countries.json

文件夹结构如下所示:

\---web
    +---crawler
    |   +---crawluniandes
    |   |   +---.settings
    |   |   +---crawluniandes
    |   |   |   +---spiders
    |   |   |   |   \---__pycache__
    |   |   |   \---__pycache__
    |   |   +---xmlnotepadsetup
    |   |   +---XSL
    |   |   \---__pycache__
    |   +---migrations
    |   |   \---__pycache__
    |   +---static
    |   |   +---css
    |   |   |   \---AMLG_CSS
    |   |   +---doc
    |   |   +---img
    |   |   |   \---buttons
    |   |   \---js
    |   |       \---vendor
    |   +---Templates
    |   +---XMLs
    |   \---__pycache__
    +---rss
    |   +---migrations
    |   |   \---__pycache__
    |   +---static
    |   |   \---css
    |   |       \---images
    |   +---Templates
    |   \---__pycache__
    +---sentimientos
    |   +---media
    |   +---migrations
    |   |   \---__pycache__
    |   +---static
    |   |   +---css
    |   |   +---externalfiles
    |   |   \---js
    |   +---templates
    |   |   \---sentimientos
    |   \---__pycache__
    +---static
    |   +---admin
    |   |   +---css
    |   |   +---fonts
    |   |   +---img
    |   |   |   \---gis
    |   |   \---js
    |   |       +---admin
    |   |       \---vendor
    |   |           +---jquery
    |   |           \---xregexp
    |   +---css
    |   |   \---AMLG_CSS
    |   +---doc
    |   +---externalfiles
    |   \---js
    |       \---vendor
    \---web
        \---__pycache__

当我检查firefox网络监视器时,json没有出现,好像js从不调用它:

Click to see the network monitor image

1 个答案:

答案 0 :(得分:0)

好吧,我发现问题是.js无法正常提供静态 因为在调用静态时添加%7B %% 20static%20'externalfiles / countries.json'%20 %% 7D。

更多细节可以在这里找到: Django gives 404 error when loading static files AFTER jquery .load is called