Monday, April 25, 2016

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

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.

On Monday, April 25, 2016 at 12:28:27 PM UTC+2, 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]
   
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/292b7116-0589-49d2-b941-66e249bd1c30%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment