On Tue, Dec 19, 2017 at 10:12 PM, Malik Rumi <[email protected]> wrote:

> I am implementing search on a local Django project: Django 1.11.5, Python
> 3.6.3, Ubuntu 16.04. My issue is getting the search results onto the
> template.
>
> I am using standard CBV for everything else in the site, but for this one
> I wrote my own. It uses the same template as my ListView, which shows all
> objects fine.
>
>
>
> def serp(request):
>     if request.method == 'GET' and 'searchbox' in request.GET:
>         q = request.GET.get('searchbox')
>     query = SearchQuery(q)
>     object_list = Entry.objects.annotate(
>         rank=SearchRank(F(
>             'search_vector'), query)).filter(
>         search_vector=query).order_by(
>         '-rank').values_list('title', 'rank')
>     return render(request, 'serp_list.html', {'object_list': object_list})
>
>
> Django Debug Toolbar reports everything went as expected. The right
> template and view were called, as well as the right db query. I had
> previously tested this query in the shell, and it pulls up a 4 element
> queryset, as it should.
>
> Even though no search results show on the page, there are 4 instances of
> the html that should be surrounding each result and rows=4 loops=1 in the
> query plan.
>
> One thing I found curious is that the **mere change** in the variable name 
> 'object_list' to 'resultslist' gave me no db query! I had always thought 
> variable names didn't matter in Python,
>
> but apparently 'object_list' is virtually a reserved word when it comes to 
> views (even FBVs) and templates in Django?
>
>
No, it is not a reserved keyword, the thing is by convention, CBV's
Listview (and others with MultipleObjectsMixin) makes available in that
variable the objects retrieved the view. It's a convention so you can
easily reuse code. What is important is the name you pass to the template,

{**'object_list'**: your_retrieved_objects}

 as it expects that. If nothing evaluates the variable which contains your
query / queryset results, it might be never executed - is that your doubt?

>  Here is the template:
>
> <blogpost start>
> <article class="blogpost">
>       <header>
>               {% for object in object_list %}
>               <h2><ahref="{{ object.get_absolute_url }}">{{ object.title 
> }}</a></h2>
>                       <div class="post-info">
>                               <span class="post-date">
>                                       <i class="icon-calendar"></i>
>                                               <span 
> class="day">{{object.chron_date}}</span>
>                                                       <span 
> class="month">{{object.clock}} </span>
>       </span>
> <span class="submitted"><i class="icon-user-1"></i> by <a href="#"></a></span>
> <span class="comments"><i class="icon-chat"></i> <a href="#">22 
> comments</a></span>
>                                                                       </div>
>       </header>
>               <div class="blogpost-content">
>                       {{ object.content|truncatewords:30 }}</div>
>                               <footer class="clearfix">
>                                       <div class="tags pull-left"><i 
> class="icon-tags"></i> <a href="#">tag 1</a>, <a href="#">tag 2</a>, <a 
> href="#">long tag 3</a></div>
>       <div class="link pull-right"><i class="icon-link"></i><a href="{{ 
> object.get_absolute_url }}">Read More</a></div>
>                               </footer>
> </article>
>               <!-- blogpost end-->
>               {% endfor %}
> Any assistance in getting the search results to show up greatly appreciated.
>
>
Your code seems fine, it seems that the object_list is not getting to the
context of the Template and the "for" is not executing due to an empty
object_list. I don't remember where is it in DDT, but there is a place
where it shows the context for the Template, look for there if there is no
empty (or not defined) "object_list"

HTH


> --
>
> ps -
>
>
> Despite the 99% similarity, this is not identical to my issue in
> https://groups.google.com/forum/#!searchin/django-users/
> template%7Csort:relevance/django-users/o4UKSTBtwyg/cU4JeRJHHgAJ There I
> had the for variable 'i' but put the name of the list in the {{regular
> variables}}. That is not the issue this time, but clearly I have trouble
> grasping the regular and efficient use of Django context and templates. And
> yes, I have looked at the docs as well as elsewhere. So if, in addition to
> an answer, you can point me to a real clear, simple, step by step primer on
> making these two things work together, I would gladly look at it.
>
>
>
>
>
>
>
>
>
> -
>
> --
> You received this message because you are subscribed to the Google Groups
> "Django users" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to [email protected].
> To post to this group, send email to [email protected].
> Visit this group at https://groups.google.com/group/django-users.
> To view this discussion on the web visit https://groups.google.com/d/
> msgid/django-users/6670bc0d-9f64-4b6f-9f62-a007d969e0b9%40googlegroups.com
> <https://groups.google.com/d/msgid/django-users/6670bc0d-9f64-4b6f-9f62-a007d969e0b9%40googlegroups.com?utm_medium=email&utm_source=footer>
> .
> For more options, visit https://groups.google.com/d/optout.
>

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/CA%2BFDnhLmgnXBXZBWhv%2B9xfjogKfTVO%3DTrZ_75MaUb91WRZ%2BwDQ%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to