djangoqueryset没有显示准确的结果(正在构建一个新闻应用程序)



我正在使用django创建一个新闻应用程序。它包括按日期搜索选项。当我选择日期(例如:2020年11月29日(并点击提交时,我会看到当天的新闻。当我尝试下面的代码而不是显示详细信息时,它会给我一个空白页。views.py

from django.shortcuts import render
from .models import *
from django.views.generic.detail import DetailView
from django.views.generic import ListView
def index(request):
return render(request,'newspaperapp/index.html')
class nowlist(ListView):
model = newsmodel_1
template_name = 'newspaperapp/index.html'
class newslist(DetailView):
model = newsmodel_1
template_name = 'newspaperapp/home.html'
context_object_name = 'newspaperapp'
# search by giving date in index and search date
class SearchView(ListView):
model = newsmodel_1
template_name = 'newspaperapp/search.html'
context_object_name = 'all_search_results'
def get_queryset(self):
result = super(SearchView, self).get_queryset()
query = self.request.GET.get('search')
if query:
postresult = newsmodel_1.objects.filter(date_published__contains=query)
result = postresult
else:
result = None
return result

urls.py

from django.urls import path
app_name = 'newspaperapp'
from .views import newslist,SearchView,nowlist
from newspaperapp import views
urlpatterns = [
path('',views.index,name='index'),
path('date/',nowlist.as_view(),name = "date"),
path('<int:pk>',newslist.as_view(),name = "home"),
path('results/', SearchView.as_view(), name='search'),
]

newspaperapp/home.html

<!DOCTYPE html>
<html lang="en" dir="ltr">
<head>
<meta charset="utf-8">
<title></title>
</head>
<body>
<p>Today's Paper</p>
{{newspaperapp.date_published}}
{{newspaperapp.category}}
</body>

newspaperapp/index.html

<!DOCTYPE html>
<html lang="en" dir="ltr">
<head>
<meta charset="utf-8">
<title></title>
</head>
<body>
<!-- this page has search option and different categories of news -->
<!-- to create search option we write views code and continue -->
<form class="form-inline my-2 my-lg-0" method="GET" action="{% url 'newspaperapp:search' %}">
<input class="form-control mr-sm-2" type="date" placeholder="Search" aria-label="Search" 
name="search">
<button class="btn btn-outline-success my-2 my-sm-0" type="submit">Search</button>
</form>
{%for newspaperapp in object_list %}
<li><a href="{%url 'newspaperapp:home' newspaperapp.pk %}">{{newspaperapp.title}}</a>
{{newspaperapp.date_published}}
{%endfor%}
</ul>
</body>
</html>

newspaperapp/search.html

{% block content %}
{% for newspaperapp in all_search_results %}
<h3><a href="{%url 'newspaperapp:home' newspaperapp.pk %}"></a></h3>
{% empty %}
<h2>No results found</h2>
{% endfor %}
{% endblock %}

Contain通常在sql中转换为LIKE,通常用于搜索列中指定的文本模式

如果要筛选日期,可以将query转换为datetime对象,并使用gtelt查找来搜索该特定日期范围内的对象。

from datetime import datetime, timedelta
class SearchView(ListView):
model = newsmodel_1
template_name = 'newspaperapp/search.html'
context_object_name = 'all_search_results'
def get_queryset(self):
result = super(SearchView, self).get_queryset()
query = self.request.GET.get('search')
# query is of type 'str', convert to datetime
start_day = datetime.fromisoformat(query)
end_day   = start_day + timedelta(days=1)
if query:
postresult = newsmodel_1.objects.filter(
date_published__gte=start_day, 
date_published__lt=end_day
)
result = postresult
else:
result = None
return result

注意:添加更多逻辑来处理queryNone

最新更新