0

I have a simple project that is using Django 1.11 and Javascript that is not working. When I run the js within the HTML, the json loads and javascripts works, but when I put all of this into the static folder, the javascript loads but nothing is executed. I have configured the static folder and it loads CSS configurations but is not working for javascript. I also run the collectstatics without success. Could you pls help?

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;
        });
      });
    }());

web/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 is located in a folder in statics with this same structure:

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

The folder structure looks like this:

\---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__

When I check the firefox network monitor, the json does not appears, as if the js never calles it:

Click to see the network monitor image

m7913d
  • 10,244
  • 7
  • 28
  • 56
vlad0x
  • 13
  • 3

1 Answers1

0

ok, I found the issue is that the static is not served properly from .js as it was adding %7B%%20static%20'externalfiles/countries.json'%20%%7D when static was called.

More details can be found here: Django gives 404 error when loading static files AFTER jquery .load is called

Community
  • 1
  • 1
vlad0x
  • 13
  • 3