Monday, April 25, 2016

FieldError: Invalid order_by arguments: ['- pub_date']

I'm following the tutorials on the Django website and now I'm stuck at tutorial 3. I'm working on the 'polls' application.

The view.py file looks like so

from django.http import HttpResponse
from django.template import loader
from django.shortcuts import render
from .models import Question


def index(request):
   
latest_question_list = Question.objects.order_by('- pub_date')[:5]
   
template = loader.get_template('polls/index.html')
    context
= { 'latest_question_list': latest_question_list, }
   
return HttpResponse(template.render(context, request))


def detail(request, question_id):
   
return HttpResponse("You're looking at question %s." % question_id)


def results(request, question_id):
    response
= "Your're looking at the results of question %s."
   
return HttpResponse(response % question_id)


def vote(request, question_id):
   
return HttpResponse("You're voting on question %s" % question_id)


where
 the "
erroneous" line is  boldfaced. This is models.py:

from django.db import models
from django.utils.encoding import python_2_unicode_compatible
from django.utils import timezone


@python_2_unicode_compatible
class Question(models.Model):
    question_text
= models.CharField(max_length = 200)
   
pub_date = models.DateTimeField('date published')
   
def __str__(self):
       
return self.question_text
   
def was_published_recently(self):
       
return self.pub_date >= timezone.now() - datetime.timedelta(days=1)


@python_2_unicode_compatible
class Choice(models.Model):
    question
= models.ForeignKey(Question, on_delete = models.CASCADE)
    choice_text
= models.CharField(max_length = 200)
    votes
= models.IntegerField(default = 0)
   
def __str__(self):
       
return self.choice_text

and the urls.py

from django.conf.urls import url


from . import views


urlpatterns
= [
    url
(r'^$', views.index, name='index'),
    url
(r'^(?P<question_id>[0-9]+)/$', views.detail, name='detail'),
    url
(r'^(?P<question_id>[0-9]+)/results/$', views.results, name='results'),
    url
(r'^(?P<question_id>[0-9]+)/vote/$', views.vote, name='vote'),
]

What's wrong? I'm using django 1.9.2 with python 2.7 and Postgresql 9.5.

--
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/f2f4baf2-4e43-45f9-83d7-f218545b43ff%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment