PythonFlask角度应用状态不刷新

2024-09-29 06:28:05 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在开发flask+Angular+MongoDb应用程序。我正在使用angular中的状态提供程序。状态从应用程序加载得非常好。但是当我刷新页面时,它抛出404错误。在

  1. 在应用程序副本:(烧瓶)
   application = Flask(__name__)
    @application.route('/')
    def showMachineList():
        print ("Inside the show machine list function")
        return application.make_response(open('templates/index.html').read())
  1. 在应用程序js(角度)
angular.module('testapp', ['ngCookies', 'ngResource',
        'ngSanitize', 'ui.router', 'ui.bootstrap','ngMaterial','ngCookies'
    ])
    .config(function($urlRouterProvider, $locationProvider,$interpolateProvider) {
        $urlRouterProvider.otherwise('/');
        //$interpolateProvider.startSymbol('//').endSymbol('//');
        $locationProvider.html5Mode(true).hashPrefix('!');
    });
angular.module('testapp')
  .config(function ($stateProvider) {
    console.log("inside create outage request")
    $stateProvider
      .state('cor', {
        url: '/cor',
        template: '<cor></cor>'
        //templateUrl: '/static/client/app/acn/acn.html'
      });
  });

我在中添加了<base href="/">索引.html也。 当我用"http://localhost:5000/cor"刷新页面时,它抛出404。 你能告诉我我缺少什么吗?在


Tags: config应用程序uiapplication状态htmlfunction页面
2条回答

刷新页面时,浏览器会将URL“http://localhost:5000/cor”发送到服务器。正如我在代码中看到的,没有定义/cor路由,只有/。我想你必须定义一个。在

@application.route('/cor')
def showMachineList():
    with open('templates/index.html') as fd:
        content = fd.read()
    return application.make_response(content)

问题可能出在您的模板中:您在其中定义了指向cor的链接。你应该有这样的东西:

^{pr2}$

我假设你写的是:

<a href="/cor">link to cor</a>

这是单页应用程序中相当常见的问题。看起来Angular应用程序可以在客户端正确地处理cor状态,但是web服务器无法知道在初始加载时,/cor路径应该路由到相同的Angular应用程序。在

尝试向终结点添加第二条路由:

@application.route('/')
@application.route('/cor')
def showMachineList():
    ...

或者,可以使用catch-all route来处理任何路径后缀。在

相关问题 更多 >