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

383 views
Skip to first unread message

gu99...@student.chalmers.se

unread,
Apr 25, 2016, 6:28:27 AM4/25/16
to Django users
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.

Michal Petrucha

unread,
Apr 25, 2016, 6:42:49 AM4/25/16
to django...@googlegroups.com
On Mon, Apr 25, 2016 at 03:28:27AM -0700, gu99...@student.chalmers.se wrote:
> 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]*

order_by does not allow spaces between the minus sign and field name,
if you use order_by('-pub_date'), it should work fine.

Cheers,

Michal
signature.asc

gu99...@student.chalmers.se

unread,
Apr 25, 2016, 7:17:08 AM4/25/16
to Django users
Yes, that worked. Someone better fix that mistake in the tutorial. Also the template for index.html is also wrong, by changing line 4 to

<li><a href="{{ question.id }}/"> {{ question.question_text }}</a></li>

it now works.

Michal Petrucha

unread,
Apr 25, 2016, 7:51:38 AM4/25/16
to django...@googlegroups.com
On Mon, Apr 25, 2016 at 04:17:08AM -0700, gu99...@student.chalmers.se wrote:
> Yes, that worked. Someone better fix that mistake in the tutorial.

I looked at the tutorial, but I didn't find any code example that
would contain an order_by call containing a space after the minus
sign. If there is such an example, could you point it out, so we can
fix it?

> Also the
> template for index.html is also wrong, by changing line 4 to
>
> <li><a href="{{ question.id }}/"> {{ question.question_text }}</a></li>
>
> it now works.

I'm not entirely certain which template you are referring to here,
since the third part of the tutorial iterates on the template for
polls/index.html, but assuming you mean the first version, both
"{{ question.id }}/" and "/polls/{{ question.id }}/" would be correct,
as long as you use the same URL patterns as described earlier in the
tutorial (i.e. if the correct URL is /polls/<id>/).

Cheers,

Michal
signature.asc

gu99...@student.chalmers.se

unread,
Apr 25, 2016, 11:49:48 AM4/25/16
to Django users
Ok, I looked at the tutorial page more closely. When you look at the code, it looks like there is a space after the minus sign, especially when you mark text. But if I copy and paste the text into some arbitrary text editor, there is no space after the minus. When I follow the tutorial, I prefer to type everything manually by hand as I believe I learn more efficiently that way.

When using "/polls/{{ question.id }}/" it doesn't work for me. In fact, the url turns into "http://127.0.0.1:8000/polls/polls/..." on my local host.

Michal Petrucha

unread,
Apr 26, 2016, 3:23:08 AM4/26/16
to django...@googlegroups.com
On Mon, Apr 25, 2016 at 08:49:47AM -0700, gu99...@student.chalmers.se wrote:
> Ok, I looked at the tutorial page more closely. When you look at the code,
> it looks like there is a space after the minus sign, especially when you
> mark text. But if I copy and paste the text into some arbitrary text
> editor, there is no space after the minus. When I follow the tutorial, I
> prefer to type everything manually by hand as I believe I learn more
> efficiently that way.

Oh, that's unfortunate, typing all samples manually instead of blindly
copy-pasting is certainly encouraged. The docs use a webfont for them,
so they are supposed to look the same everywhere, and with the default
“Fira Mono” it doesn't look like there's a space.

I'm curious – could you share a screenshot of what the code samples
look like in your browser? Could you also look in the developer tools
of your browser what font is actually being used? (In Chromium-based
browsers, you have to inspect the element containing the string, open
the computed styles, and it will show you what font was used.)

> When using "/polls/{{ question.id }}/" it doesn't work for me. In fact, the
> url turns into "http://127.0.0.1:8000/polls/polls/..." on my local host.

That shouldn't happen – are you sure that you used "/polls/{{ question.id }}/",
not "polls/{{ question.id }}/" without the leading slash?

Cheers,

Michal
signature.asc
Reply all
Reply to author
Forward
0 new messages