Django中找不到角文件
问题描述:
我有以下的HTML文件Django中找不到角文件
<html>
<head>
{% load staticfiles %}
<script type="text/javascript" src="{% static "app.js" %}"/></script>
<title>Flapper News</title>
<script src="//ajax.googleapis.com/ajax/libs/angularjs/1.2.19/angular.min.js"></script>
</head>
{% verbatim foo %}
<body ng-app="flapperNews" ng-controller="MainCtrl">
<div>
<div ng-repeat="post in posts | orderBy:'-upvotes'">
<span ng-click="incrementUpvotes(post)">^</span>
<a ng-show="post.link" href="{{post.link}}">
{{post.title}}
</a>
<span ng-hide="post.link">
{{post.title}}
</span>
- upvotes: {{post.upvotes}}
</div>
<form ng-submit="addPost()">
<input type="text" placeholder="Title" ng-model="title"></input>
<br>
<input type="text" placeholder="Link" ng-model="link"></input>
<br>
<button type="submit">Post</button>
</form>
</div>
</body>
{% endverbatim foo %}
</html>
这是我angularjs文件
'use strict'
angular.module('flapperNews', ['ngRoute'])
.controller('MainCtrl', [
'$scope',
function($scope){
$scope.test = 'Hello world!';
$scope.posts = [];
$scope.addPost = function(){
if($scope.title === '') { return; }
$scope.posts.push({
title: $scope.title,
link: $scope.link,
upvotes: 0
});
$scope.title = '';
$scope.link = '';
};
$scope.incrementUpvotes = function(post) {
post.upvotes += 1;
};
}]);
模板位于mysite的/模板和角文件位于mysite的/静态的。当我看看开发服务器请求页面时,我收到下面的输出:
[10/Jul/2015 09:23:09]"GET /guides/ HTTP/1.1" 200 861
[10/Jul/2015 09:23:09]"GET /static/app.js HTTP/1.1" 404 1632
当我看看从Firefox开发者控制台,我收到一个错误:[$注射器:modulerr。你们知道为什么我的app.js的使用不起作用吗?
编辑:在settings.py
STATIC_URL = '/static/'
STATIC_ROOT = '/home/ubuntu/mysite/static/'
答
静态文件也许你应该设置STATICFILES_DIRS
在你的项目设置。这应该做的伎俩:
STATICFILES_DIRS = (
os.path.join(BASE_DIR, 'static'),
)
这是不够的,只是设置STATIC_URL
(这是你将服务于静态文件的URL)和STATIC_ROOT
(这是你的服务器/系统的应用程序可以在位置在运行manage.py collectstatic
时存储静态文件)。
通过使用AppDirectoriesFinder
静态文件Django缺省搜索(该搜索的应用程序目录的静态文件)和FileSystemFinder
(其中搜索是在STATICFILES_DIRS
目录)
如果静态文件设置仍然不明确,看看documentation。
现在找到该文件,但收到错误“angular is not defined” –
我更新了我的答案,也解释了不同的静态设置的作用;) – jgadelange
这是由于您加载app.js '在角度之前。当你把'script'-tag放在''script''标签后加载'app.js'时,它应该可以工作。 – jgadelange