【问题标题】:TemplateSyntaxError when rendering html template using django and angularjs [duplicate]使用 django 和 angularjs 渲染 html 模板时出现 TemplateSyntaxError [重复]
【发布时间】:2016-09-06 13:25:36
【问题描述】:

Angular/django 新手在这里。我创建了一个 django 网页,并使用我的 html 模板尝试从简单的 angularjs 应用程序和控制器教程中呈现值。

尝试将数据打印到页面时,我不断从 index.html 文件中收到错误消息。

Index.html

  <div ng-app = "" ng-init = "countries = [{locale:'en-US',name:'United States'}, {locale:'en-GB',name:'United Kingdom'}, {locale:'en-FR',name:'France'}]">
    <p>Enter your Name: <input type = "text" ng-model = "name"></p>
    <p>Hello <span ng-bind = "name"></span>!</p>
    <p>List of Countries with locale:</p>

    <ol>
      <li ng-repeat = "country in countries">
        {{ 'Country: ' + country.name + ', Locale: ' + country.locale }}
      </li>
    </ol>
  </div>

  <script src = "http://ajax.googleapis.com/ajax/libs/angularjs/1.3.14/angular.min.js"></script>
  <script src="results_app/results_app/js/myApp.js"></script>
  <script src="results_app/results_app/js/myCtrl.js"></script>

错误信息:

TemplateSyntaxError at /
could not parse the remainder: ' + country.name + ', Locale: ' + country.locale' from ''Country: ' + country.name + ', Locale: ' + country.locale'

myApp.js

var myApp = angular.module('myApp',[]);

myCtrl.js

myApp.controller('ContactController', ['$scope', function($scope) {
    $scope.contacts = ["hi@email.com", "hello@email.com"];

    $scope.add = function() {
        $scope.contacts.push($scope.contact);
        $scope.contact = "";
    }
}]);

views.py

def index(request):
    return render(request, 'results_app/index.html')

视图还具有来自序列化我的模型的类视图集。

为什么会出现这个错误?我知道 html 的语法是正确的。我认为是 views.py 函数不正确?有没有不同的方式我必须通过 angularjs 呈现 html?

【问题讨论】:

    标签: javascript python html angularjs django


    【解决方案1】:

    可能有两个问题,可能是您的请求变量中没有数据,因此请尝试打印您在 render() 时返回的请求变量。或

    如果数据可用,那么您可能需要将 ng-repeat 更改为

    Country: {{country.name }}, Locale: {{country.locale }}
    

    【讨论】:

    • ng-repeat 更改不起作用。我不明白如何“打印请求变量”?
    • 只写打印请求就可以了,然后运行服务器并检查服务器日志
    猜你喜欢
    • 2017-05-21
    • 2021-05-07
    • 2023-03-10
    • 2023-03-06
    • 1970-01-01
    • 2017-01-09
    • 2017-09-05
    • 2019-11-19
    • 1970-01-01
    相关资源
    最近更新 更多