【问题标题】:flask + angularjs: ng-include is 404'ingflask + angularjs:ng-include 是 404'ing
【发布时间】:2014-06-24 15:52:11
【问题描述】:

我一直在疯狂地尝试让 angularjs 的 ng-include 与 Flask 应用程序一起正常工作,似乎无论我尝试什么,GET 都会不断返回 404'd。主页 (angular_test.html) 加载正常,但 angular_entry.html 没有。

烧瓶:

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 吗?是我的应用程序根路径设置不正确还是什么?

帮助!

【问题讨论】:

    标签: javascript python angularjs flask angularjs-ng-include


    【解决方案1】:

    如果不知道c 是什么或get 方法的作用是什么,就无法完全判断,但最有可能的是您误用了templates 参数到flask.Blueprint。如果您想将模板公开给 Angular,则需要通过 HTTP 访问它们。 Flask 的模板通过服务器端的 Jinja 模板引擎通过 render_template 提供,并且直接通过 HTTP 暴露给最终用户。

    如果这是您想要将angulartemplates 移动到static 下的问题,并更改包含路径以正确引用/static/angularetemplates/{your_template_name}(您也可以在Blueprint 初始化程序中删除关键字参数templates)。

    【讨论】:

    • 只获取应用程序的基本目录。它正在返回:'/home/jmadison/Development/FeedEater/feedeater'。我尝试更改蓝图的模板关键字,从烧瓶模板中分离出角度模板,并完全删除模板和静态关键字。我已经尝试将角度模板移动到静态文件夹。
    • @jake - 最好将该信息添加到问题中:-)
    • 哈哈是真的!但是,我找到了一个有效的组合!
      移到静态后,obvs.谢谢!!
    • 磕头是的,错过了领先的静态 - 对此表示歉意。
    猜你喜欢
    • 2015-01-21
    • 1970-01-01
    • 1970-01-01
    • 2018-09-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-05-04
    • 2021-10-03
    相关资源
    最近更新 更多