Monday, April 28, 2014

Re: IndentationError :Unexpected Indent

Ditto from what Andreas said - Python is very sensitive to indentation.  If the method 'was_published_recently' is part of the Poll model, then there should be the same amount of indentations before 'def was_published...' as for the 'def __unicode__' method.  Then, for uniformity's sake (even though it is technically not necessary), the 'return self.pub_date...' line should also have the same number of indents FROM the indent where the 'def was_published...' method line starts (in this case 3), like so:

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)

This is to indicate it is IN the 'was_published_recently' method.
Cheers.


On Monday, April 28, 2014 1:55:27 AM UTC-4, Kelvin Mwangi wrote:
 I have been following up on the tutorial on Python version 1.6 and I got stuck at an indentation error I can't seem to debug. Its under the first tutorial of creating Django application and I modified my polls\models.py file to look like this;

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

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 

I keep getting an error at that line that starts with "def was_published_recently.." and can't seem to debug the error. Can anyone kindly help me here? I'm stuck. :( 

--
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 http://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/725f3014-2e81-4af5-9ccb-41cc696191b9%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment