leora
leora

Reputation: 196449

why am i getting a python indent error here in the django sample tutorial

i have this code from the django sample tutorial

from django.db import models
from datetime import datetime

# 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_today(self):
        return self.pub_date.date() == datetime.date.today()  

and i am getting this error: IndentationError: unexpected indent

on this line:

def __unicode__(self):

any idea what is wrong ??

Upvotes: 4

Views: 9389

Answers (4)

anish
anish

Reputation: 7412

Python INDENTION will be responsible most of the time, to work correclty, use the editor or separate out the _unicode_(self) with tab

      def __unicode__(self):  # Python 3: def __str__(self):
            return str(self.name)

Upvotes: 0

Carlos
Carlos

Reputation: 31

Probably this is because you did copy/paste from the tutorial. Pay special attention to the tabs and spaces.

Hope it helps.

Upvotes: 3

rootart
rootart

Reputation: 727

Sometimes it can be hard work with big files to find where error is, so you can install pep8 http://pypi.python.org/pypi/pep8 module and use it from command line

$ pep8 a.py 
a.py:1:4: W191 indentation contains tabs
a.py:1:4: E101 indentation contains mixed spaces and tabs
a.py:1:5: E113 unexpected indentation

Upvotes: 1

My only guess is you have tabs mixed with spaces.

It's recommended to use spaces over tabs, with 4 spaces per indentation. http://www.python.org/dev/peps/pep-0008/

Do you have invisible characters visible on your editor to make sure that isn't the case?

PS: your reputation graph says -6000 and it's really bugging me out.

Upvotes: 12

Related Questions