【问题标题】:Can't load template from an Angular route in a Django project无法从 Django 项目中的 Angular 路由加载模板
【发布时间】:2018-06-14 17:45:42
【问题描述】:

在我的 urls.py 中,我有以下模式:

url(r'^$', views.index, name='index'),
url(r'^setup/$', views.index, name='index'),

两种模式最终都解析为同一个视图,因为逻辑是在控制器中处理的。视图有点像这样:

def index(request):
    return request(request, 'index.html', {})

在我的 index.html 文件中,我加载了 Angular、我的 Angular 应用程序、路由和其他所有内容。

我的角度路线看起来像这样:

.when('/', {
    controller : 'BaseController',
    templateUrl : 'static/app_partials/base.html',
    reloadOnSearch: false
})
.when('/products/', {
    controller : 'ProductsController',
    templateUrl : 'static/app_partials/products.html',
    reloadOnSearch: false
})

现在,当我转到我的模式中的第一个 URL 时,一切正常,并且路由能够完美地加载模板,因为 url 是 http://example.com/static/app_partials/base.html,这正是文件所在的位置。

但是,第二个 URL 不起作用,因为现在调用的模板是 http://example.com/setup/static/app_partials/base.html,它不存在。

我该如何解决这个问题?我试图将整个 URL 与域和内容一起放入我的路由中,但随后出现不安全的 URL 错误。有什么帮助吗?谢谢。

【问题讨论】:

  • 你可以使用绝对 URL,例如/static/app_partials/base.html 而不是像static/app_partials/base.html 这样的相对网址。
  • @Alasdair - 这解决了这个问题。非常感谢。

标签: javascript python angularjs django


【解决方案1】:

使用绝对 URL(带有前导斜杠)而不是相对 URL。

例如使用/static/app_partials/base.html 代替static/app_partials/base.html

【讨论】:

    猜你喜欢
    • 2018-04-16
    • 2018-08-07
    • 2014-07-22
    • 2018-11-17
    • 1970-01-01
    • 2015-11-10
    • 2021-04-08
    • 2020-09-18
    • 1970-01-01
    相关资源
    最近更新 更多