0

I have the following python code in a Django project:

import datetime
from django.utils import timezone
from django.db import models

# Create your models here.

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

    def __unicode__(self): 
        return self.question

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


class Choice(models.Model):
    poll = models.ForeignKey(Poll)
    choice_text = models.CharField(max_length=200)
    votes = models.IntegerField(default = 0)

    def __unicode__(self)
    return self.choice_text

When I run the python manage.py validate command I get the following error:

 def __unicode__(self)
                    ^
SyntaxError: invalid syntax

Any ideas?

2 Answers 2

3

You forget the identation and the ":" It should be :

import datetime
from django.utils import timezone
from django.db import models

# Create your models here.

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

    def __unicode__(self): 
        return self.question

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


class Choice(models.Model):
    poll = models.ForeignKey(Poll)
    choice_text = models.CharField(max_length=200)
    votes = models.IntegerField(default = 0)

    def __unicode__(self):
        return self.choice_text
Sign up to request clarification or add additional context in comments.

Comments

2

You forgot :

Change

def __unicode__(self)

to

def __unicode__(self):

Also, notice the indentation.

def __unicode__(self): 
    return u'{0}'.format(self.choice_text)
    #^ Indentation

1 Comment

Got it! I will accept answer in 10 minutes. As soon as I can accept! Thanks!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.