在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
开源软件名称:izimobil/django-rest-framework-datatables开源软件地址:https://github.com/izimobil/django-rest-framework-datatables开源编程语言:Python 90.3%开源软件介绍:django-rest-framework-datatablesOverviewThis package provides seamless integration between Django REST framework and Datatables. Install django-rest-framework-datatables, call your API with The great benefit of django-rest-framework-datatables is that you don't have to create a different API, your API still work exactly the same unless you specify the Full documentation is available on Read the Docs ! You can play with a demo of the example app on Python Anywhere. Requirements
Please note:
QuickstartInstallationJust use $ pip install djangorestframework-datatables ConfigurationTo enable Datatables support in your project, add REST_FRAMEWORK = {
'DEFAULT_RENDERER_CLASSES': (
'rest_framework.renderers.JSONRenderer',
'rest_framework.renderers.BrowsableAPIRenderer',
'rest_framework_datatables.renderers.DatatablesRenderer',
),
'DEFAULT_FILTER_BACKENDS': (
'rest_framework_datatables.filters.DatatablesFilterBackend',
),
'DEFAULT_PAGINATION_CLASS': 'rest_framework_datatables.pagination.DatatablesPageNumberPagination',
'PAGE_SIZE': 50,
} And that's it !Your API is now fully compatible with Datatables and will provide searching, filtering, ordering and pagination without any modification of your API code ! Always Serialize Specific FieldsSometimes you may want to expose fields regardless of datatable's url parameters. You can do so by setting the class ArtistSerializer(serializers.ModelSerializer):
id = serializers.IntegerField(read_only=True)
class Meta:
model = Artist
fields = (
'id', 'name',
)
datatables_always_serialize = ('id',) An example of Datatable<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Rolling Stone Top 500 albums of all time</title>
<link rel="stylesheet" href="//cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.0.0/css/bootstrap.css">
<link rel="stylesheet" href="//cdn.datatables.net/1.10.16/css/dataTables.bootstrap4.min.css">
</head>
<body>
<div class="container">
<div class="row">
<div class="col-sm-12">
<table id="albums" class="table table-striped table-bordered" style="width:100%">
<thead>
<tr>
<th>Rank</th>
<th>Artist</th>
<th>Album name</th>
<th>Year</th>
<th>Genres</th>
</tr>
</thead>
</table>
</div>
</div>
</div>
<script src="//code.jquery.com/jquery-1.12.4.js"></script>
<script src="//cdn.datatables.net/1.10.16/js/jquery.dataTables.min.js"></script>
<script src="//cdn.datatables.net/1.10.16/js/dataTables.bootstrap4.min.js"></script>
<script>
$(document).ready(function() {
var table = $('#albums').DataTable({
"serverSide": true,
"ajax": "/api/albums/?format=datatables",
"columns": [
{"data": "rank", "searchable": false},
{"data": "artist_name", "name": "artist.name"},
{"data": "name"},
{"data": "year"},
{"data": "genres", "name": "genres.name", "sortable": false},
]
});
});
</script>
</body>
</html> Example projectTo play with the example project, just clone the repository and run the dev server. $ git clone https://github.com/izimobil/django-rest-framework-datatables.git
$ cd django-rest-framework-datatables
$ pip install -r requirements-dev.txt
$ python example/manage.py runserver
$ firefox http://127.0.0.1:8000 TestingInstall development requirements. $ pip install -r requirements-dev.txt Run the tests. $ python example/manage.py test You can also use the excellent tox testing tool to run the tests against all supported versions of Python and Django. Install tox globally, and then simply run: $ tox If you want to check the coverage, use: $ coverage run ./example/manage.py test
$ coverage report -m DocumentationThe documentation is available online on Read the Docs. To build the documentation, you’ll need to install $ pip install -r requirements-docs.txt To build the documentation: $ cd docs
$ make clean && make build |
2023-10-27
2022-08-15
2022-08-17
2022-09-23
2022-08-13
请发表评论