page()接受1个位置参数,但给出了2个 [英] page() takes 1 positional argument but 2 were given
问题描述
我正在尝试设计用于搜索功能的api.搜索基于地名.我想要的是localhost:8000/api/v1/rent/search/?format = json& q ="california",但是我收到了一个错误,该错误已在下面附上
I am trying to design an api for search functionality. Search is based on place name.What i want is localhost:8000/api/v1/rent/search/?format=json&q="california" but i am getting an error which i have attached below
{
"error_message": "page() takes 1 positional argument but 2 were given",
"traceback": "Traceback (most recent call last):\n\n File \"/home/tushant/.virtualenvs/rent/lib/python3.4/site-packages/tastypie/resources.py\", line 211, in wrapper\n response = callback(request, *args, **kwargs)\n\n File \"/home/tushant/Projects/rentals-v2.1/rentals/api/api.py\", line 102, in get_search\n page = paginator.page(int(request.GET.get('page', 1)))\n\nTypeError: page() takes 1 positional argument but 2 were given\n"
}
我的搜索API代码是
from rentals.models import Rental,Gallery
from django.core.paginator import InvalidPage
from django.conf.urls import *
from tastypie.paginator import Paginator
from tastypie.exceptions import BadRequest
from tastypie.resources import ModelResource
from tastypie.utils import trailing_slash
from haystack.query import SearchQuerySet
class SearchResource(ModelResource):
class Meta:
queryset = Rental.objects.all()
resource_name = 'rent'
def prepend_urls(self):
return [
url(r"^(?P<resource_name>%s)/search%s$" % (self._meta.resource_name, trailing_slash()), self.wrap_view('get_search'), name="api_get_search"),
]
def get_search(self, request, **kwargs):
self.method_check(request, allowed=['get'])
self.is_authenticated(request)
self.throttle_check(request)
# Do the query.
sqs = SearchQuerySet().models(Rental).load_all().auto_query(request.GET.get('q', ''))
paginator = Paginator(sqs, 20)
try:
page = paginator.page(int(request.GET.get('page', 1)))
except InvalidPage:
raise Http404("Sorry, no results on that page.")
objects = []
for result in page.object_list:
bundle = self.build_bundle(obj=result.object, request=request)
bundle = self.full_dehydrate(bundle)
objects.append(bundle)
object_list = {
'objects': objects,
}
self.log_throttled_access(request)
return self.create_response(request, object_list)
我的models.py是
class Rental(models.Model):
city = models.CharField(_("City"), max_length=255, blank=False,null=True,
help_text=_("City of the rental space"))
place = models.CharField(_("Place"), max_length=255, blank=False,null=True,
help_text=_("Place of the rental space"))
class Gallery(models.Model):
rental = models.ForeignKey('Rental', null=True, on_delete=models.CASCADE,verbose_name=_('Rental'), related_name="gallery")
image = models.ImageField(blank=True,upload_to='upload/',null=True)
我想念什么?
推荐答案
您正在使用Tastypie Paginator类,该类根据传递给它的请求自动检测当前页面,这与Django Paginator类不同,该类需要您执行以下操作:传递页面索引.
You're using a Tastypie Paginator class, which automatically detects the current page based on the requests passed in to it, unlike the Django Paginator class which requires you to pass in the page index.
以下是带有相关注释的代码: https ://github.com/django-tastypie/django-tastypie/blob/master/tastypie/paginator.py#L26
Here's the code with relevant comments: https://github.com/django-tastypie/django-tastypie/blob/master/tastypie/paginator.py#L26
尝试一下:
paginator = Paginator(request.GET, sqs, limit=20)
try:
page = paginator.page()
except InvalidPage:
raise Http404("Sorry, no results on that page.")
这篇关于page()接受1个位置参数,但给出了2个的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!