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? 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. --
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.
-
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?
{**'object_list'**: your_retrieved_objects}
--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 django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django...@googlegroups.com.
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.
For more options, visit https://groups.google.com/d/optout.
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.
<blogpost start> <article class="blogpost"> <header> {% for object in object_list %} <h2><ahref="{{ object.get_absolute_url }}">{{ object.title }}</a></h2>
Here's your problem. Your results are being entered in to your header and not in to the body of the HTML. I bet if you look at the source code of the rendered page, they're all there.
--
You received this message because you are subscribed to a topic in the Google Groups "Django users" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/django-users/xSW1BjLXXSQ/unsubscribe.
To unsubscribe from this group and all its topics, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django...@googlegroups.com.
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%2Be%2BciV56ORGwDeap7nser4RwSE3gdZVXPgREMU%3Dzw8g6YDx2g%40mail.gmail.com.
FIRST, James, let me say how much I greatly appreciate you hanging in there and trying to help me.
2nd, as to your two points about the html:a. This template, serp_list.html, is identical to ktab_list.html, except that serp_list has the title 'SEARCH' at the top and some bolierplate text about this being the search results page. Other than that they are identical, including the template for loop.serp_list is supposed to be called by localhost/serp/, which points to views.serp, which is an FBV.ktab_list is a vanilla implementation of ListView, called by localhost/index/, and it shows all Entry objects as you would expect.The header you see is the blog post header, ie, this is where the title and the get absoulte url to the detail page is supposed to go. On /index/ that works like it should. On serp, well, that's why I am here.b. Your second point is probably well taken, that the for loop should completely wrap the article, and that might explain why the author and comments part of the last three posts were not the same font as the first post. I have changed that in the html, but it has not changed the result.
3rd, just because things weren't complicated enough, now I am getting inconsistent results. Whereas before I got views.serp called, a db query with 4 results, on the right template, now I am getting a value error ('returns None' - how's that for a news flash?). The only difference is that I downloaded the free version of pycharm to see if their visual debugger would help me. So calling the page through pycharm, instead of the regular terminal, is the only difference, and I don't think that should make a difference. But either way, I still have no result on my page - and oh yea, pycharm says the same thing my print statements did - that the query was there and pulled up the expected objects. They just won't show up on the template.
4th, I don't know how relevant this is, but the form which created the search box has the form action = '/serp/', but despite that, if I use the search box in /index/, I stay on /index/ and just get the query attached to the end of that url, rather than /serp/ plus the query. I have to manually type in /serp/ to get views.serp called. When it stays on /index/ plus the query, the result shown are just Entry.objects.all(), as you would expect on a ListView. Any thoughts on that? Is it related? Why isn't my form action working? THANKS!
Note this is all done through my simple html search form, defined in the template. My Django Form, SearchBox(), does absolutely nothing, as if it isn't connected to anything. I don't understand what the problem there is, I can't show that this is related or not. I do know I'd rather have the functionality than a pretty form.
An ordinary ListView of all objects works normally, as does my DetailView. I have tried several times to re-write views.serp as a cbv with get_context_data, but either context or request or Searchbox() end up being undefined, or an ImportError.
--
You received this message because you are subscribed to a topic in the Google Groups "Django users" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/django-users/xSW1BjLXXSQ/unsubscribe.
To unsubscribe from this group and all its topics, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django...@googlegroups.com.
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%2Be%2BciX3a%3D3O9AGv4hF5bff0xD7L8mCrkrrW0nKvRtNCzZ3UxQ%40mail.gmail.com.
The template context says I have a queryset
And so does the selection
I put print statements in the view:##########Quit the server with CONTROL-C.This is the value of query: SearchQuery(eaa)This is the value of QuerySet: <QuerySet [('2017-10-09 Incentive, a further review', 0.151982), ('2017-11-14 Pulling The Trigger', 0.151982), ('2017-10-05 A Failure of Incentive?', 0.121585), ('2017-10-20 spider refactor', 0.121585)]>[26/Dec/2017 18:57:31] "GET /serp/?q=eaa HTTP/1.1" 200 66580##########
Note this is all done through my simple html search form, defined in the template. My Django Form, SearchBox(), does absolutely nothing, as if it isn't connected to anything. I don't understand what the problem there is, I can't show that this is related or not. I do know I'd rather have the functionality than a pretty form.
An ordinary ListView of all objects works normally, as does my DetailView. I have tried several times to re-write views.serp as a cbv with get_context_data, but either context or request or Searchbox() end up being undefined, or an ImportError.
Finally, a screenshot of my search page, with four spaces for the 4 entries that correctly correspond to the query 'eaa'. They just aren't there.
--
You received this message because you are subscribed to a topic in the Google Groups "Django users" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/django-users/xSW1BjLXXSQ/unsubscribe.
To unsubscribe from this group and all its topics, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django...@googlegroups.com.
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%2Be%2BciWvgD7-RnqWJvpKrpYvd2SB2TGwOW2bj4UOcKbtMJVR%3Dg%40mail.gmail.com.
Your first suggested loop:
This is the first page<br>
<ul>
{% for obj in QuerySet %}
<li>{{obj}}</li>
{% empty %}
<li>No results</li>
{% endfor %}
The result:
!DOCTYPE This is the first page
('2017-10-09 Incentive, a further review', 0.151982)
('2017-11-14 Pulling The Trigger', 0.151982)
('2017-10-05 A Failure of Incentive?', 0.121585)
('2017-10-20 spider refactor', 0.121585)
Same template with your second loop added:
!DOCTYPE
<head></head>
<html>
<body>
This is the first page<br>
<ul>
{% for obj in QuerySet %}
<li>{{obj}}</li>
{% empty %}
<li>No results</li>
{% endfor %}
And now for something completely different...
<ul>
{% for obj in QuerySet %}
<li>{{obj.title}}: {{obj.chron_date}}</li>
<li>{{obj.content}}</li>
{% empty %}
<li>No results</li>
{% endfor %}
</body>
</html>
And the result:
!DOCTYPE This is the first page
('2017-10-09 Incentive, a further review', 0.151982)
('2017-11-14 Pulling The Trigger', 0.151982)
('2017-10-05 A Failure of Incentive?', 0.121585)
('2017-10-20 spider refactor', 0.121585) And now for something completely different...
:
:
:
:
<<>>
So this looks like a variable problem, but I don't claim to understand that, either. I mentioned earlier on this thread that different variables gave me different results, but everyone says that can't be it.
And the result:
!DOCTYPE This is the first page
('2017-10-09 Incentive, a further review', 0.151982)
('2017-11-14 Pulling The Trigger', 0.151982)
('2017-10-05 A Failure of Incentive?', 0.121585)
('2017-10-20 spider refactor', 0.121585) And now for something completely different...
:
:
:
:
<<>>
So this looks like a variable problem, but I don't claim to understand that, either. I mentioned earlier on this thread that different variables gave me different results, but everyone says that can't be it.
--
You received this message because you are subscribed to a topic in the Google Groups "Django users" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/django-users/xSW1BjLXXSQ/unsubscribe.
To unsubscribe from this group and all its topics, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django...@googlegroups.com.
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%2Be%2BciWjdFDwUPT7-60Gk1OfaejZeoFAsDXZpgXjp0mQ26C30g%40mail.gmail.com.