I am starting to work in the django framework and DataTables. I'm currently trying to load a jquery DataTable with data coming back from the server. I built an api using the django REST framework to transfer data to DataTables. However, I cannot load the DataTable from json data from the server. Below you will find my code snippets and requests to tell me if I am missing something.
index.html is as follows.
<table id="packages_table" class="table table-striped table-bordered">
<thead>
<tr>
<th>User Name</th>
<th>First Name</th>
<th>Email</th>
</tr>
</thead>
<tbody>
<tr>
<td></td>
<td></td>
<td></td>
</tr>
</tbody>
</table>
<script type="text/javascript">
$(document).ready(function () {
$('#packages_table').dataTable({
ajax: 'http://127.0.0.1:3434/user/',
columns: [
{ "data": "username"},
{ "data": "first_name"},
{ "data": "email"},
]
});
});
</script>
urls.py where I defined the view, the serializer and router look like this.
class UserSerializer(serializers.HyperlinkedModelSerializer):
class Meta:
model = User
fields = ('username', 'first_name', 'email', 'is_staff')
class UserViewSet(viewsets.ModelViewSet):
queryset = User.objects.all()
serializer_class = UserSerializer
router = routers.DefaultRouter()
router.register(r'user', UserViewSet)
urlpatterns = [
url(r'^admin/', include(admin.site.urls)),
url(r'^$', include(datagrid_urls)),
url(r'^', include(router.urls)),
url(r'^api-auth/', include('rest_framework.urls', namespace='rest_framework')),
] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
And below is the json data from the url.
[
{
"url": "http://127.0.0.1:3434/user/2/",
"username": "morgoth",
"first_name": "morgoth",
"email": "duke.valafar@gmail.com",
"is_staff": true
},
{
"url": "http://127.0.0.1:3434/user/3/",
"username": "anna",
"first_name": "",
"email": "anna@anna.com",
"is_staff": false
},
{
"url": "http://127.0.0.1:3434/user/4/",
"username": "adam",
"first_name": "",
"email": "ada@abc.com",
"is_staff": false
}
]
And here is my debug bookmarklet