1
我使用Django paginator,並試圖讓它在底部顯示多個頁面。目前我正在設置它,因此它只會在當前的號碼周圍顯示幾個數字(例如,前一個2 3 5 6前一個),但我知道我該如何做到這一點,但我正在努力解決django的分頁存儲page_range。它將它作爲x_range存儲,因此我不知道如何從中獲得某些值。如何從python xrange獲取特定條目 - django分頁器
middle = job_listings.paginator.page_range[before:after]
這就是我正在嘗試做的事情,但它會返回以下錯誤。任何人都可以告訴我如何從xrange中獲得一系列物品?
Environment:
Request Method: GET
Request URL: http://127.0.0.1:8000/listings/browse?page=4
Django Version: 1.9.1
Python Version: 2.7.10
Installed Applications:
('django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'django.contrib.sites',
'main',
'listings',
'profiles',
'allauth',
'allauth.account',
'allauth.socialaccount')
Installed Middleware:
('django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.auth.middleware.SessionAuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware')
Traceback:
File "C:\Python27\Lib\site-packages\django\core\handlers\base.py" in get_response
149. response = self.process_exception_by_middleware(e, request)
File "C:\Python27\Lib\site-packages\django\core\handlers\base.py" in get_response
147. response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "D:\Other folders\Desktop\Student Job Search\code\opus_jobs_project\listings\views.py" in browse
54. middle = job_listings.paginator.page_range[before:after]
Exception Type: TypeError at /listings/browse
Exception Value: sequence index must be integer, not 'slice'
可能相關的分頁代碼 -
job_listings_list = JobListing.objects.filter(filters).distinct().order_by('-listing_date')
print(job_listings_list)
paginator = Paginator(job_listings_list, 1) # Show 25 contacts per page
page = request.GET.get('page')
try:
job_listings = paginator.page(page)
except PageNotAnInteger:
# If page is not an integer, deliver first page.
job_listings = paginator.page(1)
except EmptyPage:
# If page is out of range (e.g. 9999), deliver last page of results.
job_listings = paginator.page(paginator.num_pages)
current_page_start = job_listings.start_index()
current_page_end = job_listings.end_index()
before_current_pages=1
after_current_pages=1
before = max(job_listings.number - before_current_pages, 0)
after = job_listings.number + after_current_pages
middle = job_listings.paginator.page_range[before:after]
print("before", before, "after", after)
(讓我知道如果你需要更多的代碼)