Python page()接受1个位置参数,但给出了2个
我正在尝试为搜索功能设计一个api。搜索是基于地名的。我想要的是localhost:8000/api/v1/rent/Search/?format=json&q=“california”,但我得到了一个错误,我在下面附上了这个错误 我的搜索api代码是Python page()接受1个位置参数,但给出了2个,python,django,tastypie,django-1.9,Python,Django,Tastypie,Django 1.9,我正在尝试为搜索功能设计一个api。搜索是基于地名的。我想要的是localhost:8000/api/v1/rent/Search/?format=json&q=“california”,但我得到了一个错误,我在下面附上了这个错误 我的搜索api代码是 from rentals.models import Rental,Gallery from django.core.paginator import InvalidPage from django.conf.urls
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)
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类要求您传入页面索引 以下是代码和相关注释: 试试这个:
paginator = Paginator(request.GET, sqs, limit=20)
try:
page = paginator.page()
except InvalidPage:
raise Http404("Sorry, no results on that page.")
您应该在问题中包含完整的堆栈跟踪。请将堆栈跟踪作为文本而不是imagepaginator添加。page()不接受任何参数:您可以看到文档,这里传递了一个参数。请注意:我必须将
更改为页面中的结果。对象列表:
更改为页面中的结果['objects']:
这有效。我没有收到名为“whoosh.codec.whoosh2”的模块错误,我将解决它。谢谢你的帮助。
paginator = Paginator(request.GET, sqs, limit=20)
try:
page = paginator.page()
except InvalidPage:
raise Http404("Sorry, no results on that page.")