"Polls" app in Django Beginners' Tutorial

115 views
Skip to first unread message

David Manlove

unread,
Jan 19, 2017, 2:21:32 PM1/19/17
to Django users

I have a question about the "Polls" app, as described in the Django Beginners' Tutorial, https://docs.djangoproject.com/en/1.10/intro/.  In tutorial 3, the index view contains the following code:

 

latest_question_list = Question.objects.order_by('-pub_date')[:5]

 

It is stated that this produces the latest 5 questions according to publication date.  The '-' in front of 'pub_date' is intended to return the questions in descending rather than ascending order of publication date.  Yet when I tried this out, it returns the *earliest* 5 questions in my database.  Is this an error, or (more likely), have I done something wrong?

 

Thanks for any advice!

 

David

jorr...@gmail.com

unread,
Jan 20, 2017, 5:00:00 AM1/20/17
to Django users
Perhaps you forgot the '-' before pub_date?
Message has been deleted

DjangoUserDM

unread,
Jan 20, 2017, 7:33:33 AM1/20/17
to Django users
No, definitely didn't!

jorr...@gmail.com

unread,
Jan 20, 2017, 8:08:28 AM1/20/17
to Django users
Can you show your Question model?


On Friday, January 20, 2017 at 1:33:33 PM UTC+1, DjangoUserDM wrote:
No, definitely didn't!

DjangoUserDM

unread,
Jan 20, 2017, 8:30:37 AM1/20/17
to Django users
Here it is:

class Question(models.Model):
    question_text = models.CharField(max_length=200)
    pub_date = models.DateTimeField('date published')

    def was_published_recently(self):
        now = timezone.now()
        return now - datetime.timedelta(days=1) <= self.pub_date <= now

    def __str__(self):
        return self.question_text

jorr...@gmail.com

unread,
Jan 20, 2017, 9:17:00 AM1/20/17
to Django users
Did you set a pub_date on the questions you created? Since the field doesn't have a default value it will remain empty and have no effect on ordering unless you explicitly set pub_date=timezone,now() every time you create a question.

DjangoUserDM

unread,
Jan 20, 2017, 1:50:24 PM1/20/17
to Django users
Yes, all questions had pub_date set either by a population script or by the admin interface.  Anyway, it's working as it should.  My problem was that the question I thought was published most recently (i.e., Jan 2017) had in fact been set with a publication date of Jan 2016.  Apologies for wasting anybody's time!  But the answers here helped to figure this out - thanks.

David

Vpeguero

unread,
Jan 21, 2017, 2:21:27 AM1/21/17
to Django users
I believe this is how the code is supposed to function. When you visit the index page the list should show the most recently posted/edited poll question.
For example:

Lets say we have 3 questions

   Question 1, published - 1 / 19 / 2017 - morning
  
   Question 2, published - 1 / 19 / 2017 - night

   Question 3, published - 1 / 20 / 2017 

When we call Question.objects.all() is passes the published date of each question model and lists them in ascending order. 
This means that the most recently edited or posted question will be on the bottom of the list and our list would like something like so:

   Question 3, Question, 2, Question 1

Now by adding '-' this tell django that we want to index our list in reverse order. Since everything is stored in ascending order as it is created, it is easy to look up things in descending order by reversing our list.
In this case the list isn't literally reversed merely indexed from the end of the list to the beginning.

So with that said if we call Question.objects.all('-pub_date') it will return to us our list in reverse order

Question 1, Question 2, Question 3. 
 ( The 5 within the brackets will return everything before the 5th index, index 0-4.)

Okay now with all that in mind I would recommend that you check out the polls admin page. On the Questions page i would delete all of the Questions that have already been created and i would recommend creating at least 6 questions either manually 
or using a script to create a dummy database. Make sure that the pub date for each question is one day apart. 

Then in your polls.views under latest_question_list add 
    
   oldest_to_newest = Question.objects.all('pub_date')[:5]

 make sure to include oldest_to_newest in the context dictionary ( context { 'item1': item1, 'item2': item2, }

With that complete edit index.html to look like so.
polls/templates/polls/index.html
{% if latest_question_list %}
   
<ul>
   
{% for question in latest_question_list %}
       
<li><a href="/polls/{{ question.id }}/">{{ question.question_text }}</a></li>
   
{% endfor %}
   
</ul>
{% else %}
   
<p>No polls are available.</p>
{% endif %}

<br />

{%
if oldest_to_newest %}
   
<ul>
   
{% for question in oldest_to_newest %}
       
<li><a href="/polls/{{ question.id }}/">{{ question.question_text }}</a></li>
   
{% endfor %}
   
</ul>
{% else %}
   
<p>No polls are available.</p>
{% endif %}

Now when you go to /polls you should see two lists the first in descending order and the second in ascending order and if you open your admin page in a new tab you can check the pub date of your Questions and compare that information to
what is being rendered so that you can more easily see what django is doing. 

Bottom line i believe there was a bit of a misunderstanding as to how the list would be rendered, between what you imagined it should look like and what the django tutorial explains will be rendered. Hopefully this will help you better understand whats going on
a bit better. I myself have gotten stuck on some parts of the tutorial and found that I've usually misread or misunderstood something in my haste or have created a bug while trying to type fast. 

Again hope this help, 
Victor

P.s. If the official tutorial leaves you with some questions and you need more information to feel confident while developing with django, I would recommend checking out tango with django and even django girls, they are both very indepth, and are focused on
building a website ready to publish from start to finish to help better understand the entire process of web-development and how django makes it easier to develop websites. Also spending time finding other django applications and learning how to use them with
your projects, for example bootstraps3, sekizai, and django-cms are a few applications you can install and include in your projects to help with the development process. 
Reply all
Reply to author
Forward
0 new messages