Django分页,下一个,上一个,计数 [英] Django pagination with next, previous, count
问题描述
我正在尝试通过此链接进行分页和阅读.
I am trying to do for pagination and reading through this link.
使用API-View在Django-Rest-Framework中进行分页
我需要具有下一个先前的分页网址.问题是当我称这个为
I need to have next, previous url for pagination. Problem is that when I call this one,
pagination_class = settings.DEFAULT_PAGINATION_CLASS
说
设置"对象没有属性"DEFAULT_PAGINATION_CLASS"
我需要导入什么?或安装?
What do I need to import? or install ?
Models.py
Models.py
class CarView(APIView):
permission_classes = ()
def get(self, request):
""" Get all car """
car = Car.objects.all()
# paginator = PageNumberPagination()
serializer = CarSerializer(car)
serialized_car = CarSerializer(car, context={"request": request})
# serializer = CarSerializer(car[0])
return Response(serialized_car.data)
Serializers.py
Serializers.py
class CarSerializer(serializers.ModelSerializer):
photo_url = serializers.SerializerMethodField('get_photo_url')
class Meta:
model = Car
fields = ('id','name','price', 'photo_url')
def get_photo_url(self, car):
request = self.context.get('request')
photo_url = car.photo.url
return request.build_absolute_uri(photo_url)
settings.py
settings.py
REST_FRAMEWORK = {
'DEFAULT_PAGINATION_CLASS': 'rest_framework.pagination.PageNumberPagination',
'PAGE_SIZE': 100,
'DEFAULT_AUTHENTICATION_CLASSES':
('rest_framework.authentication.OAuth2Authentication',
'rest_framework.authentication.SessionAuthentication'),
'DEFAULT_MODEL_SERIALIZER_CLASS':
'rest_framework.serializers.ModelSerializer',
'DEFAULT_PERMISSION_CLASSES':
('rest_framework.permissions.IsAdminUser',)
}
推荐答案
您的设置文件未包含DEFAULT_PAGINATION_CLASS
的定义,但确实包含了REST_FRAMEWORK
的定义,并且DEFAULT_PAGINATION_CLASS
是其中的一部分.
Your settings file doesn't include a definition for DEFAULT_PAGINATION_CLASS
, it does include a definition for REST_FRAMEWORK
, and DEFAULT_PAGINATION_CLASS
is part of it.
有关访问
From Django Rest Framework documentation on accessing settings
访问设置
如果您需要访问REST的值 项目中框架API的设置,您应该使用 api_settings对象.例如.
If you need to access the values of REST framework's API settings in your project, you should use the api_settings object. For example.
from rest_framework.settings import api_settings
print api_settings.DEFAULT_AUTHENTICATION_CLASSES
api_settings对象将检查是否有任何用户定义的设置,并且 否则倒下 回到默认值.任何使用字符串导入路径的设置 引用一个类将自动导入并返回 引用的类,而不是字符串文字.
The api_settings object will check for any user-defined settings, and otherwise fall back to the default values. Any setting that uses string import paths to refer to a class will automatically import and return the referenced class, instead of the string literal.
您应该可以访问api_settings.DEFAULT_PAGINATION_CLASS
这篇关于Django分页,下一个,上一个,计数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!