Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 618
leora Avatar asked Feb 05 '11 19:02

leora


3 Answers

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.

like image 73
Yuji 'Tomita' Tomita Avatar answered Oct 12 '22 22:10

Yuji 'Tomita' Tomita


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

Hope it helps.

like image 20
Carlos Avatar answered Oct 12 '22 23:10

Carlos


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
like image 39
rootart Avatar answered Oct 13 '22 00:10

rootart