django template 中使用drf中的serializer、Pagination、Filter
时间: 2024-05-14 13:12:27 浏览: 178
django rest framework使用django-filter用法
1. 使用serializer
在django template中,可以通过调用drf中的serializer来渲染数据。首先,需要在view中定义好serializer,并将查询到的数据序列化后传递给模板。例如:
```python
from rest_framework import serializers
class BookSerializer(serializers.ModelSerializer):
class Meta:
model = Book
fields = ['id', 'title', 'author', 'publish_date']
def book_list(request):
books = Book.objects.all()
serializer = BookSerializer(books, many=True)
return render(request, 'book_list.html', {'books': serializer.data})
```
然后,在模板中使用django的模板语言展示序列化后的数据:
```html
{% for book in books %}
<div>
<h3>{{ book.title }}</h3>
<p>{{ book.author }}</p>
<p>{{ book.publish_date }}</p>
</div>
{% endfor %}
```
2. 使用Pagination
如果需要在django template中使用drf中的分页功能,可以通过调用Pagination类来实现。首先,需要在view中定义好pagination,并将查询到的数据和pagination对象传递给模板。例如:
```python
from rest_framework.pagination import PageNumberPagination
class BookPagination(PageNumberPagination):
page_size = 10
page_size_query_param = 'page_size'
max_page_size = 100
def book_list(request):
books = Book.objects.all()
pagination = BookPagination()
result_page = pagination.paginate_queryset(books, request)
serializer = BookSerializer(result_page, many=True)
return render(request, 'book_list.html', {'books': serializer.data, 'paginator': pagination})
```
然后,在模板中使用django的模板语言展示分页后的数据:
```html
{% for book in books %}
<div>
<h3>{{ book.title }}</h3>
<p>{{ book.author }}</p>
<p>{{ book.publish_date }}</p>
</div>
{% endfor %}
<div class="pagination">
<span class="step-links">
{% if page_obj.has_previous %}
<a href="?page=1">« first</a>
<a href="?page={{ page_obj.previous_page_number }}">previous</a>
{% endif %}
<span class="current-page">
Page {{ page_obj.number }} of {{ paginator.num_pages }}
</span>
{% if page_obj.has_next %}
<a href="?page={{ page_obj.next_page_number }}">next</a>
<a href="?page={{ paginator.num_pages }}">last »</a>
{% endif %}
</span>
</div>
```
3. 使用Filter
如果需要在django template中使用drf中的过滤功能,可以通过调用FilterSet类来实现。首先,需要在view中定义好filter,并将查询到的数据和filter对象传递给模板。例如:
```python
from django_filters import rest_framework as filters
class BookFilter(filters.FilterSet):
title = filters.CharFilter(lookup_expr='icontains')
author = filters.CharFilter(lookup_expr='icontains')
class Meta:
model = Book
fields = ['title', 'author']
def book_list(request):
books = Book.objects.all()
book_filter = BookFilter(request.GET, queryset=books)
serializer = BookSerializer(book_filter.qs, many=True)
return render(request, 'book_list.html', {'books': serializer.data, 'filter': book_filter})
```
然后,在模板中使用django的模板语言展示过滤后的数据:
```html
<form method="get">
<label for="title">Title:</label>
<input type="text" id="title" name="title" value="{{ filter.form.title.value }}">
<label for="author">Author:</label>
<input type="text" id="author" name="author" value="{{ filter.form.author.value }}">
<button type="submit">Filter</button>
</form>
{% for book in books %}
<div>
<h3>{{ book.title }}</h3>
<p>{{ book.author }}</p>
<p>{{ book.publish_date }}</p>
</div>
{% endfor %}
```
阅读全文