[英]Django - TypeError: object of type 'NoneType' has no len() occurred while doing Pagination
TypeError: object of type 'NoneType' has no len()
當我向列表視圖添加分頁時TypeError: object of type 'NoneType' has no len()
。 當我點擊next
按鈕轉到第2頁時,就會發生這種情況。它應該還有兩個結果要顯示。
我想這是因為值search
擦拭或queryset
變成無不知何故,當我點擊下一步(到2頁),然后返回None。
書/瀏覽次數:
class SearchResultView(generic.ListView):
template_name = 'book/search.html'
model = Book
context_object_name = 'book_list'
paginate_by = 2
def get_queryset(self):
queryset = super().get_queryset()
search = self.request.GET.get('search')
if search:
return Book.objects.filter(
title__icontains=search
)
書/模板/ book.html:
<form class="form-inline my-2 my-lg-0" method="get" action="{% url 'book:search' %}">{% csrf_token %}
<input style="font-size: 12px; width: 200px" class="form-control mr-sm-2" name="search" type="search" placeholder="Book Name" aria-label="Search">
<button class="btn btn-outline-primary my-2 my-sm-0" type="submit">Search</button>
</form>
書/模板/ search.html:
<div id="customContents">
{% block customContents %}
{% if book_list %}
<ul>
{% for book in book_list %}
<li>{{ book.title }}</li>
{% empty %}
{% endfor %}
</ul>
{% if is_paginated %}
<div class="pagination">
<span class="page-links">
{% if page_obj.has_previous %}
<a href="?page={{ page_obj.previous_page_number }}">previous</a>
{% endif %}
{% if page_obj.has_next %}
<a href="?page={{ page_obj.next_page_number }}">next</a>
{% endif %}
</span>
</div>
{% endif %}
{% else %}
<h3> No result found. </h3>
{% endif %}
{% endblock %}
</div>
書/網址:
app_name = 'book'
urlpatterns = [
path('', views.HomePageView.as_view(), name='home'),
path('search/', views.SearchResultView.as_view(), name='search'),
path('<slug:slug>/', views.BookDetailView.as_view(), name='detail'),
]
它只能正常工作:
class SearchResultView(generic.ListView):
template_name = 'book/search.html'
model = Book
context_object_name = 'book_list'
paginate_by = 2
queryset = Book.objects.filter(
title__icontains='Python'
)
您覆蓋查詢字符串中的search
參數,因為您沒有重復它。 在這個關於如何處理這個問題的問題中 ,隨着時間的推移積累了許多答案。
這是你修理的錯誤:
<a href="?page={{ page_obj.previous_page_number }}">previous</a>
(對於“下一個”鏈接也一樣)。
然后一切都應該像宣傳的那樣工作。
或者,您可以將搜索參數作為URL本身的一部分,但這會產生使搜索可緩存的副作用(根據您的站點的不同而不好),並且需要使用javascript將輸入移動到URL。
要完全在Django中執行此操作,您需要將搜索結果傳輸到RedirectView ,該RedirectView除了驗證搜索參數外,不會執行任何其他操作,然后重定向到實際的搜索列表。
同樣,這種方法有一些副作用,最明顯的是對后退按鈕的影響。
因此,我建議您在鏈接的答案中查看查詢字符串修改,如果您想要使用其他路線,請根據您的偏好發布另一個問題。
你的get_queryset()
方法返回None
。 因為不是在查詢字符串中傳遞search
,而是傳遞page=2
。 因此,您的search
變量為None
。 如果條件失敗,則get_queryset()
返回None
。
將return queryset.filter(title='no result found')
置於條件之外。
def get_queryset(self):
queryset = super().get_queryset()
search = self.request.GET.get('search')
if search:
books_with_title = queryset.filter(title__icontains=search)
if len(books_with_title) > 0:
return books_with_title
books_with_author = queryset.filter(authors__name=search)
if len(books_with_author) > 0:
return books_with_author
books_with_publisher = queryset.filter(publisher__name=search)
if len(books_with_publisher) > 0:
return books_with_publisher
return queryset.filter(title='no result found')
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.