Django Custom Pagination - 在settings.py中设置为默认值

时间:2022-02-03 08:34:58

6.30.15 - HOW CAN I MAKE THIS QUESTION BETTER AND MORE HELPFUL TO OTHERS? FEEDBACK WOULD BE HELPFUL. THANKS!

6.30.15 - 我如何使这个问题更好,更有益于其他人?反馈会有所帮助。谢谢!

I have a custom pagination I need to set for my Django Rest Framework. I have created a pagination.py file and placed the pagination settings there under a class.

我有一个我需要为我的Django Rest Framework设置的自定义分页。我创建了一个pagination.py文件,并将分页设置放在一个类下面。

In Settings.py I have to point to it using the 'DEFAULT_PAGINATION_CLASS'. However, I can not get settings.py to find it. Even though I've specified the path. In the documentation I noticed there's a similar 'apps.core.pagination' path - do I have to create this? Where do I need to place this so the settings.py can read it?

在Settings.py中,我必须使用'DEFAULT_PAGINATION_CLASS'指向它。但是,我找不到settings.py来找到它。即使我已经指定了路径。在文档中,我注意到有类似的'apps.core.pagination'路径 - 我是否必须创建它?我需要在哪里放置这个,所以settings.py可以读取它?

'DEFAULT_PAGINATION_CLASS': 
'XXXX_XX_api.static.rest_framework.pagination.CustomPagination' 

UPDATE: 6.22.15 I added the custom pagination to my settings file, but now I'm getting a weird error. And I'm not sure why. Everything seems to be correct in my code. In the error it reads the custom pagination but it says that pagination is not defined. Any thoughts?

更新:6.22.15我将自定义分页添加到我的设置文件中,但现在我收到了一个奇怪的错误。而且我不确定为什么。在我的代码中,一切似乎都是正确的。在错误中它读取自定义分页,但它表示分页没有定义。有什么想法吗?

 File "/home/dbadmin/epic_ar_api/epic_ar_api/epic_ar_api/pagination.py", line 5, in <module>
    class CustomPagination(pagination.LimitOffsetPagination):
NameError: name 'pagination' is not defined
[22/Jun/2015 13:29:24] "GET /machines/ HTTP/1.1" 500 59
^Cdbadmin@ubuntu:~/epic_ar_api/epic_ar_api$ 

1 个解决方案

#1


1  

You need to add the DEFAULT_PAGINATION_CLASS settings in the global settings.py file in the REST_FRAMEWORK settings dictionary.

您需要在REST_FRAMEWORK设置字典的全局settings.py文件中添加DEFAULT_PAGINATION_CLASS设置。

Suppose my CustomPagination class resides in pagination.py file in my_app of my_project. Then i can do the following:

假设我的CustomPagination类驻留在my_project的my_app中的pagination.py文件中。然后我可以做以下事情:

   REST_FRAMEWORK = {
        'DEFAULT_PAGINATION_CLASS': 'my_project.my_app.pagination.CustomPagination',
        'PAGE_SIZE': 100
    }

# Update:

#更新:

Add the following line to your pagination.py file:

将以下行添加到pagination.py文件中:

from rest_framework import pagination

This will import the default rest_framework pagination.

这将导入默认的rest_framework分页。

#1


1  

You need to add the DEFAULT_PAGINATION_CLASS settings in the global settings.py file in the REST_FRAMEWORK settings dictionary.

您需要在REST_FRAMEWORK设置字典的全局settings.py文件中添加DEFAULT_PAGINATION_CLASS设置。

Suppose my CustomPagination class resides in pagination.py file in my_app of my_project. Then i can do the following:

假设我的CustomPagination类驻留在my_project的my_app中的pagination.py文件中。然后我可以做以下事情:

   REST_FRAMEWORK = {
        'DEFAULT_PAGINATION_CLASS': 'my_project.my_app.pagination.CustomPagination',
        'PAGE_SIZE': 100
    }

# Update:

#更新:

Add the following line to your pagination.py file:

将以下行添加到pagination.py文件中:

from rest_framework import pagination

This will import the default rest_framework pagination.

这将导入默认的rest_framework分页。