'NoneType' 类型的 Django Pagination 对象没有 len()
Django Pagination object of type 'NoneType' has no len()
view.py
def search(request):
query_string = ''
found_entries = None
if ('q' in request.GET) and request.GET['q'].strip():
query_string = request.GET['q']
entry_query = get_query(query_string, ['id', 'sample_name'])
found_entries = Sample.objects.filter(entry_query).exclude(status_id=3).order_by('id')
paginator = Paginator(found_entries, 30)
page = request.GET.get('page')
try:
sample = paginator.page(page)
except PageNotAnInteger:
sample = paginator.page(1)
except EmptyPage:
sample = paginator.page(paginator.num_pages)
return render_to_response('search_results.html', { 'sample' : sample })
search_result.html
{% extends 'base.html' %}
{% block content %}
<table class="reference" style="width:100%">
<tr>
<th>Tracking ID</th>
<th>Sample Name</th>
<th>External ID</th>
<th>Owner Name</th>
<th>Custodian Name</th>
<th>Feedstock</th>
<th>Treatment</th>
<th>Fraction</th>
<th>Create Date</th>
<th>Update</th>
<th>Delete</th>
</tr>
{% for result in sample %}
<tr>
<td>{{result.id}}</td>
<td>{{result.sample_name}}</td>
<td>{{result.external_id}}</td>
<td>{{result.owner_name}}</td>
<td>{{result.custodian_name}}</td>
<td>{{result.feedstock.feedstock_name}}</td>
<td>{{result.treatment.treatment_name}}</td>
<td>{{result.fraction.fraction_name}}</td>
<td>{{result.create_date}}</td>
<td><a href="/sdms/update/{{result.id}}">update</a></td>
<td><a href="/sdms/delete/{{result.id}}">delete</a></td>
</tr>
{% endfor %}
<div class="pagination">
<span class="step-links">
{% if sample.has_previous %}
<a href="?page={{ sample.previous_page_number }}">previous</a>
{% endif %}
<span class="current">
Page {{ sample.number }} of {{ sample.paginator.num_pages }}
</span>
{% if sample.has_next %}
<a href="?page={{ sample.next_page_number }}">next</a>
{% endif %}
</span>
</div>
初始页面显示正确。 table 中显示了 30 个项目。它还显示 10 页中的第 1 页,下一页显示 link。当我单击 link 时出现错误:'NoneType' 类型的对象没有 len()
这是回溯:
File "c:\python27\lib\site-packages\django-1.7.1-py2.7.egg\django\core\handlers\base.py" in get_response
111. response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "C:\Users\vtran.NREL_NT\PycharmProjects\sdms\sdms\sample\views.py" in search
66. sample = paginator.page(page)
File "c:\python27\lib\site-packages\django-1.7.1-py2.7.egg\django\core\paginator.py" in page
50. number = self.validate_number(number)
File "c:\python27\lib\site-packages\django-1.7.1-py2.7.egg\django\core\paginator.py" in validate_number
39. if number > self.num_pages:
File "c:\python27\lib\site-packages\django-1.7.1-py2.7.egg\django\core\paginator.py" in _get_num_pages
86. if self.count == 0 and not self.allow_empty_first_page:
File "c:\python27\lib\site-packages\django-1.7.1-py2.7.egg\django\core\paginator.py" in _get_count
77. self._count = len(self.object_list)
您的 next
link 不包含查询字符串,因此当您单击它时,生成的下一个请求会留下 found_entries = None
,然后您将其传递到 Paginator
这需要一个项目列表。
您已将 link 设置为 ?page=whatever
。所以您只发送 link 中的页码。但是您的视图会检查是否存在 q
参数,并且只有在存在时才会覆盖 found_entries
.
的值
您需要在 next/previous 页 link 中保留 q
参数。
view.py
def search(request):
query_string = ''
found_entries = None
if ('q' in request.GET) and request.GET['q'].strip():
query_string = request.GET['q']
entry_query = get_query(query_string, ['id', 'sample_name'])
found_entries = Sample.objects.filter(entry_query).exclude(status_id=3).order_by('id')
paginator = Paginator(found_entries, 30)
page = request.GET.get('page')
try:
sample = paginator.page(page)
except PageNotAnInteger:
sample = paginator.page(1)
except EmptyPage:
sample = paginator.page(paginator.num_pages)
return render_to_response('search_results.html', { 'sample' : sample })
search_result.html
{% extends 'base.html' %}
{% block content %}
<table class="reference" style="width:100%">
<tr>
<th>Tracking ID</th>
<th>Sample Name</th>
<th>External ID</th>
<th>Owner Name</th>
<th>Custodian Name</th>
<th>Feedstock</th>
<th>Treatment</th>
<th>Fraction</th>
<th>Create Date</th>
<th>Update</th>
<th>Delete</th>
</tr>
{% for result in sample %}
<tr>
<td>{{result.id}}</td>
<td>{{result.sample_name}}</td>
<td>{{result.external_id}}</td>
<td>{{result.owner_name}}</td>
<td>{{result.custodian_name}}</td>
<td>{{result.feedstock.feedstock_name}}</td>
<td>{{result.treatment.treatment_name}}</td>
<td>{{result.fraction.fraction_name}}</td>
<td>{{result.create_date}}</td>
<td><a href="/sdms/update/{{result.id}}">update</a></td>
<td><a href="/sdms/delete/{{result.id}}">delete</a></td>
</tr>
{% endfor %}
<div class="pagination">
<span class="step-links">
{% if sample.has_previous %}
<a href="?page={{ sample.previous_page_number }}">previous</a>
{% endif %}
<span class="current">
Page {{ sample.number }} of {{ sample.paginator.num_pages }}
</span>
{% if sample.has_next %}
<a href="?page={{ sample.next_page_number }}">next</a>
{% endif %}
</span>
</div>
初始页面显示正确。 table 中显示了 30 个项目。它还显示 10 页中的第 1 页,下一页显示 link。当我单击 link 时出现错误:'NoneType' 类型的对象没有 len()
这是回溯:
File "c:\python27\lib\site-packages\django-1.7.1-py2.7.egg\django\core\handlers\base.py" in get_response
111. response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "C:\Users\vtran.NREL_NT\PycharmProjects\sdms\sdms\sample\views.py" in search
66. sample = paginator.page(page)
File "c:\python27\lib\site-packages\django-1.7.1-py2.7.egg\django\core\paginator.py" in page
50. number = self.validate_number(number)
File "c:\python27\lib\site-packages\django-1.7.1-py2.7.egg\django\core\paginator.py" in validate_number
39. if number > self.num_pages:
File "c:\python27\lib\site-packages\django-1.7.1-py2.7.egg\django\core\paginator.py" in _get_num_pages
86. if self.count == 0 and not self.allow_empty_first_page:
File "c:\python27\lib\site-packages\django-1.7.1-py2.7.egg\django\core\paginator.py" in _get_count
77. self._count = len(self.object_list)
您的 next
link 不包含查询字符串,因此当您单击它时,生成的下一个请求会留下 found_entries = None
,然后您将其传递到 Paginator
这需要一个项目列表。
您已将 link 设置为 ?page=whatever
。所以您只发送 link 中的页码。但是您的视图会检查是否存在 q
参数,并且只有在存在时才会覆盖 found_entries
.
您需要在 next/previous 页 link 中保留 q
参数。