如何在 Django 4 中显示搜索结果列表
How to display a list of search results in Django 4
我正在编写一个 Django 应用来管理销售线索。从列出潜在客户页面的模板中,我想让用户能够使用名字和姓氏作为搜索条件来搜索所有潜在客户。当我输入姓氏并按 Enter 键时,页面会闪烁但仍保持在同一页面上,而不是我创建的用于列出搜索结果的模板。
我的观点、网址和模板如下。我错过了什么?
views.py
class LeadsListView(ListView):
model = Lead
template_name = 'leads/list.html'
context_object_name = 'leads_list'
paginate_by = 10
def get_queryset(self):
return Lead.objects.order_by('id')
class LeadsSearchResultsView(ListView):
model = Lead
context_object_name = 'search_results'
template_name = 'leads/search_results.html'
paginate_by = 10
def get_queryset(self):
query = self.request.GET.get('q')
return Lead.objects.filter(
Q(last__icontains=query) | Q(first__icontains=query)
)
urls.py
from django.urls import path
from .views import LeadsListView, LeadsDetailView, LeadsCreateView
from .views import LeadsSearchResultsView
app_name = 'lead'
urlpatterns = [
path('', LeadsListView.as_view(), name='list'),
path('<int:pk>/', LeadsDetailView.as_view(), name='detail'),
path('', LeadsCreateView.as_view(), name='create'),
path('', LeadsSearchResultsView.as_view(), name='search_results'),
]
(成功!)列出潜在客户的模板,list.html:
{% extends 'base.html' %}
{% block content %}
<nav aria-label="Page navigation example">
<ul class="pagination">
{% if page_obj.has_previous %}
<li class="page-item">
<a class="page-link" href="?page={{ page_obj.previous_page_number }}" aria-label="Previous">
<span aria-hidden="true">«</span>
<span class="sr-only">Previous</span>
</a>
</li>
{% endif %}
{% if page_obj.has_next %}
<li class="page-item">
<a class="page-link" href="?page={{ page_obj.next_page_number }}" aria-label="Next">
<span aria-hidden="true">»</span>
<span class="sr-only">Next</span>
</a>
</li>
{% endif %}
<form action="{% url 'lead:search_results' %}" method="get">
<input type="search" name="q" value="{{ request.GET.q }}" placeholder="Search by last name"
class="form-control">
</form>
</ul>
</nav>
<table class="table table-hover table-bordered">
<thead>
<tr>
<th scope="col">Lead No.</th>
<th scope="col">First</th>
<th scope="col">Last</th>
<th scope="col">Phone</th>
<th scope="col">Email</th>
<th scope="col">Zip Code</th>
</tr>
</thead>
<tbody>
{% for lead in page_obj %}
<tr>
<td><a href="{% url 'lead:detail' lead.pk %}">{{ lead.pk }}</a></td>
<td>{{ lead.first }}</td>
<td>{{ lead.last }}</td>
<td>{{ lead.mobile }}</td>
<td>{{ lead.email }}</td>
<td>{{ lead.zipcode }}</td>
</tr>
{% endfor %}
</tbody>
</table>
{% endblock content %}
最后,应该列出搜索结果但没有列出的模板,search_results.html:
{% extends 'base.html' %}
{% block content %}
<nav aria-label="Page navigation example">
<ul class="pagination">
{% if page_obj.has_previous %}
<li class="page-item">
<a class="page-link" href="?page={{ page_obj.previous_page_number }}" aria-label="Previous">
<span aria-hidden="true">«</span>
<span class="sr-only">Previous</span>
</a>
</li>
{% endif %}
{% if page_obj.has_next %}
<li class="page-item">
<a class="page-link" href="?page={{ page_obj.next_page_number }}" aria-label="Next">
<span aria-hidden="true">»</span>
<span class="sr-only">Next</span>
</a>
</li>
{% endif %}
<form action="{% url 'lead:search_results' %}" method="get">
<input type="search" name="q" value="{{ request.GET.q }}" placeholder="Search by last name"
class="form-control">
</form>
</ul>
</nav>
<table class="table table-hover table-bordered">
<thead>
<tr>
<th scope="col">Lead No.</th>
<th scope="col">First</th>
<th scope="col">Last</th>
<th scope="col">Phone</th>
<th scope="col">Email</th>
<th scope="col">Zip Code</th>
</tr>
</thead>
<tbody>
{% for lead in page_obj %}
<tr>
<td><a href="{% url 'lead:detail' lead.pk %}">{{ lead.pk }}</a></td>
<td>{{ lead.first }}</td>
<td>{{ lead.last }}</td>
<td>{{ lead.mobile }}</td>
<td>{{ lead.email }}</td>
<td>{{ lead.zipcode }}</td>
</tr>
{% endfor %}
</tbody>
</table>
{% endblock content %}
下面是您的 url.py 文件
urlpatterns = [
path('', LeadsListView.as_view(), name='list'),
path('<int:pk>/', LeadsDetailView.as_view(), name='detail'),
path('', LeadsCreateView.as_view(), name='create'),
path('', LeadsSearchResultsView.as_view(), name='search_results'),
]
在这里,您用相同的 url '' 调用了 3 个不同的视图 class,这是错误的。
您可以更正并继续。
我正在编写一个 Django 应用来管理销售线索。从列出潜在客户页面的模板中,我想让用户能够使用名字和姓氏作为搜索条件来搜索所有潜在客户。当我输入姓氏并按 Enter 键时,页面会闪烁但仍保持在同一页面上,而不是我创建的用于列出搜索结果的模板。
我的观点、网址和模板如下。我错过了什么?
views.py
class LeadsListView(ListView):
model = Lead
template_name = 'leads/list.html'
context_object_name = 'leads_list'
paginate_by = 10
def get_queryset(self):
return Lead.objects.order_by('id')
class LeadsSearchResultsView(ListView):
model = Lead
context_object_name = 'search_results'
template_name = 'leads/search_results.html'
paginate_by = 10
def get_queryset(self):
query = self.request.GET.get('q')
return Lead.objects.filter(
Q(last__icontains=query) | Q(first__icontains=query)
)
urls.py
from django.urls import path
from .views import LeadsListView, LeadsDetailView, LeadsCreateView
from .views import LeadsSearchResultsView
app_name = 'lead'
urlpatterns = [
path('', LeadsListView.as_view(), name='list'),
path('<int:pk>/', LeadsDetailView.as_view(), name='detail'),
path('', LeadsCreateView.as_view(), name='create'),
path('', LeadsSearchResultsView.as_view(), name='search_results'),
]
(成功!)列出潜在客户的模板,list.html:
{% extends 'base.html' %}
{% block content %}
<nav aria-label="Page navigation example">
<ul class="pagination">
{% if page_obj.has_previous %}
<li class="page-item">
<a class="page-link" href="?page={{ page_obj.previous_page_number }}" aria-label="Previous">
<span aria-hidden="true">«</span>
<span class="sr-only">Previous</span>
</a>
</li>
{% endif %}
{% if page_obj.has_next %}
<li class="page-item">
<a class="page-link" href="?page={{ page_obj.next_page_number }}" aria-label="Next">
<span aria-hidden="true">»</span>
<span class="sr-only">Next</span>
</a>
</li>
{% endif %}
<form action="{% url 'lead:search_results' %}" method="get">
<input type="search" name="q" value="{{ request.GET.q }}" placeholder="Search by last name"
class="form-control">
</form>
</ul>
</nav>
<table class="table table-hover table-bordered">
<thead>
<tr>
<th scope="col">Lead No.</th>
<th scope="col">First</th>
<th scope="col">Last</th>
<th scope="col">Phone</th>
<th scope="col">Email</th>
<th scope="col">Zip Code</th>
</tr>
</thead>
<tbody>
{% for lead in page_obj %}
<tr>
<td><a href="{% url 'lead:detail' lead.pk %}">{{ lead.pk }}</a></td>
<td>{{ lead.first }}</td>
<td>{{ lead.last }}</td>
<td>{{ lead.mobile }}</td>
<td>{{ lead.email }}</td>
<td>{{ lead.zipcode }}</td>
</tr>
{% endfor %}
</tbody>
</table>
{% endblock content %}
最后,应该列出搜索结果但没有列出的模板,search_results.html:
{% extends 'base.html' %}
{% block content %}
<nav aria-label="Page navigation example">
<ul class="pagination">
{% if page_obj.has_previous %}
<li class="page-item">
<a class="page-link" href="?page={{ page_obj.previous_page_number }}" aria-label="Previous">
<span aria-hidden="true">«</span>
<span class="sr-only">Previous</span>
</a>
</li>
{% endif %}
{% if page_obj.has_next %}
<li class="page-item">
<a class="page-link" href="?page={{ page_obj.next_page_number }}" aria-label="Next">
<span aria-hidden="true">»</span>
<span class="sr-only">Next</span>
</a>
</li>
{% endif %}
<form action="{% url 'lead:search_results' %}" method="get">
<input type="search" name="q" value="{{ request.GET.q }}" placeholder="Search by last name"
class="form-control">
</form>
</ul>
</nav>
<table class="table table-hover table-bordered">
<thead>
<tr>
<th scope="col">Lead No.</th>
<th scope="col">First</th>
<th scope="col">Last</th>
<th scope="col">Phone</th>
<th scope="col">Email</th>
<th scope="col">Zip Code</th>
</tr>
</thead>
<tbody>
{% for lead in page_obj %}
<tr>
<td><a href="{% url 'lead:detail' lead.pk %}">{{ lead.pk }}</a></td>
<td>{{ lead.first }}</td>
<td>{{ lead.last }}</td>
<td>{{ lead.mobile }}</td>
<td>{{ lead.email }}</td>
<td>{{ lead.zipcode }}</td>
</tr>
{% endfor %}
</tbody>
</table>
{% endblock content %}
下面是您的 url.py 文件
urlpatterns = [
path('', LeadsListView.as_view(), name='list'),
path('<int:pk>/', LeadsDetailView.as_view(), name='detail'),
path('', LeadsCreateView.as_view(), name='create'),
path('', LeadsSearchResultsView.as_view(), name='search_results'),
]
在这里,您用相同的 url '' 调用了 3 个不同的视图 class,这是错误的。
您可以更正并继续。