Exception Value: Could not parse the remainder: '().order_by('-votes')' from 'poll.altword_set.all().order_by('-votes')'

1,256 views
Skip to first unread message

Pepsodent Cola

unread,
Aug 7, 2013, 5:59:38 PM8/7/13
to django...@googlegroups.com
Hi,
I have some database query ordering problems.

1.)
On my webpage I see this output:

* Option-1 = 18 votes
* Option-2 = 50 votes


2.)
altword_list.html

<ul>
{% for choice2 in poll.altword_set.all %}
<!-- for choice2 in poll.altword_set.all().order_by('-votes') -->
    <li>{{ choice2.rosword }} - {{ choice2.alt_ros_word }} - {{ choice2.alt_transl_word }} - {{ choice2.articulate }} = {{ choice2.votes }} votes</li>
{% endfor %}
</ul>


3.)
I want my webpage to order the output by most votes down to least votes like so:

* Option-2 = 50 votes
* Option-1 = 18 votes


4.)
In Python shell I managed to accomplish that by doing like so:

>>> p = Word.objects.get(pk=1)
>>> p
<Word: Page>
>>> p.altword_set.all()
[<Altword: Leafy 2>, <Altword: Leafy 3>]

>>> p.altword_set.all().order_by('votes')
[<Altword: Leafy 2>, <Altword: Leafy 3>]


5.)
How come when I replace this:
    {% for choice2 in poll.altword_set.all %}
to this:
    {% for choice2 in poll.altword_set.all().order_by('-votes')  %}
then I get this error:

Exception Type: TemplateSyntaxError
Exception Value: 
Could not parse the remainder: '().order_by('-votes')' from 'poll.altword_set.all().order_by('-votes')'


How do I write the correct syntax for ordering my list?

Pepsodent Cola

unread,
Aug 7, 2013, 6:08:21 PM8/7/13
to django...@googlegroups.com
I wrote one line wrong at:

4.)
>>> p.altword_set.all().order_by('-votes')
[<Altword: Leafy 3>, <Altword: Leafy 2>]


How do I write the correct syntax for ordering my list?




Ramiro Morales

unread,
Aug 7, 2013, 6:32:13 PM8/7/13
to django...@googlegroups.com
On Wed, Aug 7, 2013 at 6:59 PM, Pepsodent Cola <pepsod...@gmail.com> wrote:
> How come when I replace this:
> {% for choice2 in poll.altword_set.all %}
> to this:
> {% for choice2 in poll.altword_set.all().order_by('-votes') %}
> then I get this error:
>
> Exception Type: TemplateSyntaxError
> Exception Value:
>
> Could not parse the remainder: '().order_by('-votes')' from
> 'poll.altword_set.all().order_by('-votes')'
>
>
>
> How do I write the correct syntax for ordering my list?

You can't. The template language only supports calling methods without
arguments, and in that case the () aren't needed.

Please read this section of the documentation fully for more details:

https://docs.djangoproject.com/en/1.5/topics/templates/#accessing-method-calls

You will need to implement the ordering with a method in another layer
and invoke it from the template layer.

Regards,

--
Ramiro Morales
@ramiromorales

Pepsodent Cola

unread,
Aug 8, 2013, 4:54:03 PM8/8/13
to django...@googlegroups.com
I'm still a bit confused how to make my ordering work, I can't find examples that order things the way I want :(

1.)
My webpage now look like this after playing with the Viewpage.

localhost:8000/navi_polls/madara/1/

<h1>Page</h1>

<!-- Filter 5 -->
* Page - Leafy 2 = 18 votes
* Page - Leafy 3 = 50 votes

<!-- Filter 4b -->
* Paper - Leaf = 300 votes
* Page - Leafy 3 = 50 votes
* Page - Leafy 2 = 18 votes
* Paper - Leafy = 0 votes


2.)
altword_list.html

<h1>{{ poll.rosword }}</h1>

<!-- Filter 5 -->
<ul>
<!-- poll.altword_set.all.order_by('-votes') -->
{% for choice in poll.altword_set.all %}
    <li>{{ choice.rosword }} - {{ choice.alt_ros_word }} - {{ choice.alt_transl_word }} - {{ choice.articulate }} = {{ choice.votes }} votes</li>
{% endfor %}
</ul>

<!-- Filter 4b -->
<ul>
{% for choice in filter_4b %}
    <li>{{ choice.rosword }} - {{ choice.alt_ros_word }} - {{ choice.alt_transl_word }} - {{ choice.articulate }} = {{ choice.votes }} votes</li>
{% endfor %}
</ul>


3.)
How can I make Filter 5 behave like Filter 4b?


4.)
index.html

<article>
<h1>Filter 2</h1><!-- Filter 2 -->
<h3>(Word)</h3>

{% if filter_2 %}
    <ul>
    {% for row in filter_2 %}
        <li><a href="{% url 'navi_polls:altword_list' row.id %}">{{ row.rosword }}</a></li>
    {% endfor %}
    </ul>
{% else %}
    <p>No polls are available.</p>
{% endif %}
</article>


5.)
IndexView

class IndexView(generic.ListView):
    template_name = 'navi_polls/index.html'
    context_object_name = 'latest_poll_list'

    def get_queryset(self):
# Filter 1
        """
        Return the last five published polls
        (not including those set to be published in the future).
        """
        return Word.objects.filter(pub_date__lte=timezone.now()
                ).order_by('-pub_date')[:5]

        #"""
        #Return the last five published polls
        #(including those set to be published in the future).
        #"""
        #return Word.objects.order_by('-pub_date')[:5]

    def get_context_data(self, **kwargs):
        context = super(IndexView, self).get_context_data(**kwargs)
# Filter 2
        filter_2 = Word.objects.filter(direct_transl_word='')
# Filter 4
        filter_4 = Altword.objects.filter(rosword__direct_transl_word='')

        context.update({
            "filter_2": filter_2,
            "filter_4": filter_4
        })
        return context


6.)
AltwordlistView

class AltwordlistView(generic.DetailView):
    model = Word
    template_name = 'navi_polls/altword_list.html'
    context_object_name = 'poll'

    def get_queryset(self):
        #"""
        #Excludes any polls that aren't published yet.
        #"""
        #return Word.objects.filter(pub_date__lte=timezone.now())
# Filter 5
        return Word.objects.filter(direct_transl_word='')
        #return Altword.objects.filter(rosword__direct_transl_word='')


    def get_context_data(self, **kwargs):
        context = super(AltwordlistView, self).get_context_data(**kwargs)
# Filter 4b
        filter_4b = Altword.objects.filter(rosword__direct_transl_word='').order_by('-votes')

        context.update({
            "filter_4b": filter_4b
        })
        return context


7.)
Models

class Word(models.Model):
    rosword = models.CharField(max_length=200)
    direct_transl_word = models.CharField(max_length=120, blank=True, null=True)
    pub_date = models.DateTimeField('date published')

    def __unicode__(self):
        return self.rosword

    def was_published_recently(self):
        now = timezone.now()
        return now - datetime.timedelta(days=1) <= self.pub_date < now
        # Enables future date selection.
        #return self.pub_date >= timezone.now() - datetime.timedelta(days=1)
    was_published_recently.admin_order_field = 'pub_date'
    was_published_recently.boolean = True
    was_published_recently.short_description = 'Published recently?'
#_______________________________________________________________________________

class Altword(models.Model):
    rosword = models.ForeignKey(Word)
    alt_ros_word = models.CharField(max_length=200)
    alt_transl_word = models.CharField(max_length=200)
    articulate = models.CharField(max_length=200)
    votes = models.IntegerField(default=0)
    pub_date = models.DateTimeField('date published')

    def __unicode__(self):
        return self.alt_ros_word
#_______________________________________________________________________________







On Wednesday, August 7, 2013 11:59:38 PM UTC+2, Pepsodent Cola wrote:
Reply all
Reply to author
Forward
0 new messages