user3105927 user3105927 - 5 months ago 53
AngularJS Question

Django template not rendering JSON response when used with AngularJS

EDIT: Solved

views.py

def post_list(request):
queryset = Post.objects.all()
json_data = serializers.serialize('json', queryset)

context = {
"jsondata" : json_data,
}

return render(request,"index.html", context)


index.html





<script src="https://cdnjs.cloudflare.com/ajax/libs/semantic-ui/1.11.8/semantic.min.js"></script>
<script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.min.js"></script>

<body ng-app="myapp" ng-controller="myctrl">

{{ jsondata }}

<div class="ui icon input">
<input type="text" ng-model="search" placeholder="Search skills...">
<i class="search link icon"></i>
</div>


<div class="ui link cards" style="padding:40px">

<div class="card">

<div class="image">
<img class="ui avatar centered image" src="http://1.semantic-ui.com/images/avatar/large/elliot.jpg">
</div>

<div class="content">

<div class="ui small header" ng-repeat="skill in skills | filter:search">
{{ skill.fields.post_title}}
</div>

<div class="description">
{{ skill.fields.post_content }}
</div>

</div>
</div>
</div>

<script type="text/javascript">
var skills_list = "{{ jsondata }}";

var nice_data = JSON.parse(skills_list.replace(/&quot;/g, '"'))

var very_nice_data = JSON.stringify(nice_data);

console.log(very_nice_data)
</script>
<script>
angular.module('skillboard', []).controller('searchskills', function ($scope) {
$scope.skills = very_nice_data;
});
</script>
</body>


Output of
**very_nice_data**
in console is:

[
{
"model": "posts.post",
"pk": 1,
"fields": {
"post_title": "Algorithms",
"post_content": "Calling it.",
"updated_on": "2016-06-12T09:09:45.198Z",
"timestamp": "2016-04-20T09:44:21.887Z",
"test_type": "Coding",
"number_of_questions": 0,
"test_url": "http://example.com"
}
},
{
"model": "posts.post",
"pk": 4,
"fields": {
"post_title": "Data Structures",
"post_content": "new content here",
"updated_on": "2016-06-12T09:09:26.359Z",
"timestamp": "2016-04-26T06:28:32.569Z",
"test_type": "Coding",
"number_of_questions": 0,
"test_url": "http://example.com"
}
},
{
"model": "posts.post",
"pk": 11,
"fields": {
"post_title": "Dynamic Programming",
"post_content": "This level of DP is well suited for 2+ yr experience programmers/researchers.",
"updated_on": "2016-06-12T09:09:16.542Z",
"timestamp": "2016-06-12T08:44:25.705Z",
"test_type": "Coding",
"number_of_questions": 0,
"test_url": "#"
}
}
]


I am trying to render JSON response from my django view into my template using angular. I am using semantic cards for each item. JSON response is perfectly fine. ng-repeat is also looping for number of items in the JSON but post_title and post_content is not displaying.

<div class="ui small header" ng-repeat="skill in skills | filter:search">
{{ skill.fields.post_title }}
</div>

<div class="description">
{{ skill.fields.post_content }}
</div>


Where is the bug? Please help.

Answer

Finally resolve the issue. I encountered to issue below:

There was conflict in the curly braces of django and angular view bindings. So i created custom bindings below.

    var app=angular.module('appName', []);
    app.config(function($interpolateProvider) {
    $interpolateProvider.startSymbol('[[');
    $interpolateProvider.endSymbol(']]');
    });