0

I am reading django official tutorial ( http://docs.djangoproject.com/en/dev/intro/tutorial01/ ). When i try run "python manage.py shell", python throw error:

File "D:\DjangoProjects\mysite\polls\models.py", line 8
   def __unicode__(self):
   ^
IndentationError: unexpected indent

Help please! How solve this problem?

models.py:

import datetime
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

class Choice(models.Model):
    poll = models.ForeignKey(Poll)
    choice = models.CharField(max_length=200)
    votes = models.IntegerField()   
def __unicode__(self):
    return self.choice
KillaBee
  • 21
  • 1
  • This clearly isn't the code you're running, as the line you've indicated has no indent at all in the pasted code. Please update the question so the code looks exactly the same as in your editor. – Daniel Roseman Mar 03 '11 at 21:54

1 Answers1

2

You have a python indentation error. I'm not exactly sure where, but the django docs show this: http://docs.djangoproject.com/en/dev/intro/tutorial01/

So follow it to the letter /space.

class Poll(models.Model):
    # ...
    def __unicode__(self):  
        return self.question

Your exact indentation you've pasted wouldn't throw that error, but in your real code, you must have the def __unicode__ line at the exact indent depth as the other lines in your model.

Make sure you are using spaces and not tabs for all of your indents, as tabs can sometimes make the indent level seem the same as the others.

Yuji 'Tomita' Tomita
  • 115,817
  • 29
  • 282
  • 245