flask + angularjs: ng-include 404 错误
我一直在拼命尝试让AngularJS的ng-include在Flask应用中正常工作,但无论我怎么做,GET请求总是返回404错误。主页面(angular_test.html)加载得很好,但angular_entry.html却加载不出来。
Flask:
basedir = c.get('basedir')
app = Blueprint('angulartest', __name__, static_folder=basedir+'/display/static',
template_folder=basedir+'/display/angulartemplates')
def before_blueprint_request():
g.user = current_user
app.before_request(before_blueprint_request)
@app.route('/angular_test.html', methods=['GET'])
def ang_test():
print 'testing angular'
return make_response(open(basedir+'/display/templates/angular_test.html').read())
#
# return app.send_static_file('angular_test.html') (I've tried both)
HTML:
<div id='content' ng-app='FeedEaterApp' ng-controller='MainController'>
<select ng-model="template" ng-options="t.name for t in templates">
<option value="">(blank)</option>
</select>
url of the template: <tt>{{template.url}}</tt>
<div ng-include src="'angular_entry.html'"></div>
<div ng-include src="template.url"></div>
<div ng-include="'angular_entry.html'"></div>
<div ng-include="template.url"></div>
</div>
JS:
app.controller("MainController", function($scope){
$scope.message = 'this is a message from scope!~'
$scope.templates =
[ { name: 'template1.html', url: 'angular_entry.html'},
{ name: 'template2.html', url: 'template2.html'} ];
$scope.template = $scope.templates[0];
});
我也试过所有我能想到的相对路径和绝对路径,但还是只得到404错误。我还尝试过在Flask中更改静态文件和模板文件夹的位置,但都没有用。
Flask是不是在干扰对angular_entry.html的GET请求?我的应用根路径设置得不对吗?
求助!
1 个回答
2
要完全理解这个问题,我们需要知道c
是什么,以及它的get
方法具体做什么。不过,最有可能的情况是你在使用flask.Blueprint
的templates
参数时搞错了。如果你想让Angular能够访问这些模板,它们需要通过HTTP可以被访问。Flask的模板是在服务器端通过render_template
和Jinja模板引擎处理的,并不是直接通过HTTP暴露给最终用户的。
如果真的是这个问题,你需要把angulartemplates
放到static
文件夹下,并且修改你的引用路径,正确地指向/static/angularetemplates/{your_template_name}
(你也可以在Blueprint
的初始化时去掉templates
这个关键字参数)。