Jeffr A.
Jeffr A.

Reputation: 23

Django - getting a syntax error in a class generic views

Right now I`m learning django and going through documentation. And when I try to use generic views it gives me an exeption:

  File "/home/jeffr/Рабочий стол/codetry/mysite1/polls/views.py", line 8
     def IndexView(generic.ListView):
                          ^
  SyntaxError: invalid syntax

This is my views.py:

    from django.views import generic

    from .models import Choice, Question

    def IndexView(generic.ListView):
        template_name = 'polls/index.html'
         contest_object_name = 'latest_question_list'

    get_queryset(self):
        """Return the last five published questions"""
        return Question.objects.order_by('-pub_date')[:5]

    def DetailView(generic.DetailView):
        model = Question
        template_name = 'polls/detail.html'

The full traceback paste can be found here: http://dpaste.com/3QMN3A0

Any help would be greatly appreciated, Thanks

Upvotes: 2

Views: 1336

Answers (1)

willeM_ Van Onsem
willeM_ Van Onsem

Reputation: 477814

The def keyword means you are implementing a function. But here you are not specifying a function, but a class. You define a class with the class keyword, like:

from django.views import generic

from .models import Choice, Question

class IndexView(generic.ListView):
    template_name = 'polls/index.html'
     contest_object_name = 'latest_question_list'

    def get_queryset(self):
        """Return the last five published questions"""
        return Question.objects.order_by('-pub_date')[:5]

class DetailView(generic.DetailView):
    model = Question
    template_name = 'polls/detail.html'

This raises an error since functions can take parameters, but the parameter names, can not contain dots.

You also forgot to use a def to define the get_queryset method.

Upvotes: 4

Related Questions