Reputation: 2234
I'm learning Django through the tutorials on their website and I'm running into a weird problem. At this step when I get to the part where I enter the unicode snippets so that
>>> Poll.objects.all()
will return not this
[<Poll: Poll object>]
but something like this
[<Poll: What's up?>]
for some reason the code only works when I copy and paste it in, and not when I type it in. Any ideas why this is happening?
::
So here is the code that wouldn't format in the comments:
from django.db import models
import datetime
class Poll(models.Model):
question = models.CharField(max_length=200)
pub_date = models.DateTimeField('date published')
def was_published_today(self):
return self.pub_date.date() ==datetime.date.today()
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
Upvotes: 0
Views: 621
Reputation: 11
If you use TextMate, check Soft Tabs : 4 and use the command Cleanup WhiteSpaces. It worked 4 met!
Upvotes: 1
Reputation: 2160
Must be caused by mixed tab/space indentation...
Your code pasted in the comment was messed up but I had a look at the HTML source code and found that the lines you typed in (around the __unicode__
methods, specifically) were indented using mixed tabs/spaces. Maybe you're using an editor where you configured the "tab width" to be 4 so that the a tab indentation level looks the same as 4 spaces. However the python interpreter considers a tab equivalent as 8 spaces (two indent levels). So the lines you typed (or lines with tabs) are wrongly indented.
Here I mark all the tabs in your code with "<T>
"
from django.db import models
import datetime
class Poll(models.Model):
question = models.CharField(max_length=200)
pub_date = models.DateTimeField('date published')
def was_published_today(self):
<T> return self.pub_date.date() ==datetime.date.today()
<T> def __unicode__(self):
<T> <T> return self.question
class Choice(models.Model):
poll = models.ForeignKey(Poll)
choice = models.CharField(max_length=200)
votes = models.IntegerField()
def __unicode__(self):
<T> <T> return self.choice
NEVER mix tabs and spaces, in any language. And in Python we always use 4 spaces to indent as recommended by PEP-8.
Whatever editor you use, google for how to configure it to automatically expand tabs into 4 spaces.
Upvotes: 1