Tuesday, December 26, 2017

Re: Search results in template

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.


"None of you has faith until he loves for his brother or his neighbor what he loves for himself."

On Tue, Dec 26, 2017 at 8:34 PM, Malik Rumi <malik.a.rumi@gmail.com> wrote:
Here is the Entry model:

class Entry(models.Model):
    title = models.CharField(max_length=100, blank=False, null=False)
    slug = models.SlugField(max_length=100)
    content = models.TextField()
    posted_date = models.DateTimeField(auto_now=True)
    chron_date = models.DateField(auto_now=False, auto_now_add=False, blank=True)
    clock = models.TimeField(auto_now=False, auto_now_add=False, blank=True)

    ALIGN = "Align"
    CULTURE = "Culture"
    EXPENSE = "Expense"
    INCOME = "Income"
    HUMAN_RELATIONSHIPS = "Human Relationships"
    CATEGORY_CHOICES = (
        (ALIGN, "Align"),
        (CULTURE, "Culture"),
        (EXPENSE, "Expense"),
        (INCOME, "Income"),
        (HUMAN_RELATIONSHIPS, "Human Relationships"),
    )
    category = models.CharField(max_length=25, choices=CATEGORY_CHOICES, default=INCOME)
    tags = models.ManyToManyField(Tag)
    search_vector = SearchVectorField(blank=True, null=True)

    class Meta:
        verbose_name = "Diary Entry"
        verbose_name_plural = "Diary Entries"
        ordering = ["-chron_date", "clock"]
        indexes = [
            GinIndex(fields=['search_vector'])
        ]

    def __str__(self):
        return self.title

    def get_absolute_url(self):
        return reverse('detail', kwargs={"slug": self.slug})


You already have the template in my original post to start this thread.

"None of you has faith until he loves for his brother or his neighbor what he loves for himself."

On Tue, Dec 26, 2017 at 2:26 PM, James Schneider <jrschneider83@gmail.com> wrote:



The template context says I have a queryset

I see it there. Can you post up the model for Entry?




And so does the selection

Yes. It appears that you will have 4 results in your queryset.



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

##########

At this point, the view doesn't appear to be the problem. We need to start tracking things down in the template code.




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.


No reason you can't have both, but let's work on the result display issue first.

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.


Are you overriding get_context_data() correctly? It doesn't sound like it.


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.

This is where the problem is, I think.

You should set up a simple loop in your template:

<ul>
{% for obj in QuerySet %}
<li>{{obj}}</li>
{% empty %}
<li>No results</li>
{% endfor %}

If that works, you should go deeper per your Entry model:


<ul>
{% for obj in QuerySet %}
<li>{{obj.name}}: {{obj.weight}}</li>
{% empty %}
<li>No results</li>
{% endfor %}

I'm using name and weight as an attribute of Entry, but you should use something that matches your model definition. I think the issue is that you are not referring to your model correctly within the search results within the template.

-James

--
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-users@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.

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 django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@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/CAKd6oBxqp83LhTanYh0Dz5KKu-FT82VoYOc3K3fcvNcrRoNQEQ%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment