Monday, April 28, 2014

Re: IndentationError :Unexpected Indent

Based on the code below it looks like your indentation is off for def was_published recently code block (i.e. 3 spaces instead of 4). If that does not solve the problem, you might also check to ensure that you're not mixing tabs and spaces when indenting. Depending on your environment that can lead to some confusing errors. For example, if you have a tab in one line, and multiple spaces in the next line (of the same code block), in your editor it might appear visually that the indentation is the same, but the interpreter may treat them differently.

Different folks prefer tabs vs. spaces, but the key is to be consistent. google's style guide calls for 4 spaces:

https://google-styleguide.googlecode.com/svn/trunk/pyguide.html#Indentation

R,
Dow


On Apr 28, 2014, at 6:54 AM, Rene Zelaya <rene.zelaya.f@gmail.com> wrote:

> 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.

--
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/913E6FEB-A50B-4BD3-93A5-23200C539D58%40prodigy.net.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment