News

django queryset not showing accurate result (building a news app)

I am creating a news app using django. It consists of search by date option. When i choose the date(ex:29-11-2020) and click submit, It should take me to the news of that particular day. When i try the below code instead of showing the details it is giving me a blank page.
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 %}

Leave a Reply

Your email address will not be published. Required fields are marked *

Time limit is exhausted. Please reload the CAPTCHA.