#1
  1. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2013
    Posts
    46
    Rep Power
    2

    Django Tutorial Woes


    I'm doing the Django Tutorial from their page.

    It says to add this code:

    Code:
    class Poll(models.Model):
        # ...
        def __unicode__(self):  # Python 3: def __str__(self):
            return self.question
    
    class Choice(models.Model):
        # ...
        def __unicode__(self):  # Python 3: def __str__(self):
            return self.choice_text
    And now I'm getting an error message on line 2 (it's line 2 in this code that I present here. There's more code in the actual file. )

    Does it have anything to do with the __ underscores?
    I'm using 2 of them and I tried 1 also. Didn't work.

    What do you guys think is going on that's causing this error message?

    *EDIT* I'm using the CMD line and the error message actually shows a ^ pointing to the colon character.
  2. #2
  3. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2013
    Posts
    16
    Rep Power
    0
    Please post the entire traceback. Extremely difficult to help in this case.
  4. #3
  5. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2013
    Posts
    46
    Rep Power
    2
    First I'll mention that there is no Traceback. It just doesn't do what it's supposed to do.

    These are two classes that define the models of data for the app.
    Code:
    from django.db import models
    import datetime
    
    class Poll(models.Model):
        question = models.CharField(max_length=200)
        pub_date = models.DateTimeField('date published')
        def __unicode__(self):  # Python 3: def __str__(self):
            return self.question
        def was_published_recently(self):
            return self.pub_date.date() == datetime.date.today()
    
    class Choice(models.Model):
        poll = models.ForeignKey(Poll)
        choice_text = models.CharField(max_length=200)
        votes = models.IntegerField(default=0)
        def __unicode__(self):  # Python 3: def __str__(self):
            return self.choice_text
    Adding the code for unicode here:
    Code:
    def __unicode__(self):
        return self.question
    Is supposed make the Django API call return a more informative information about your objects. Here's the API call:
    Code:
    # objects.all() displays all the polls in the database. 
    >>> Poll.objects.all()
    This is the non informative return of Poll.objects.all() is:
    Code:
    >>> Poll.objects.all()
     [<Poll: Poll object>]
    The more informative answer which it's supposed to display after adding the unicode method is this:
    Code:
    >>> Poll.objects.all()
    [<Poll: What's up?>]
    The problem/error I'm experiencing is that it still displays the non informative answer.
  6. #4
  7. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2013
    Posts
    16
    Rep Power
    0
    Which version of python are you using? Try defining ___str__ as well with the body matching that of your Unicode function.
  8. #5
  9. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2013
    Posts
    46
    Rep Power
    2
    I'm using Python 2.7 and Django 1.3 because that's my web hosting service uses.

    Do you mean putting __str__ like this?

    Code:
    class Poll(models.Model):
        question = models.CharField(max_length=200)
        pub_date = models.DateTimeField('date published')
        def __unicode__(self):  # Python 3: def __str__(self):
            return self.question
        def __str__(self):
            return self.question
        def was_published_recently(self):
            return self.pub_date.date() == datetime.date.today()
  10. #6
  11. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2013
    Posts
    16
    Rep Power
    0
    Yup! Did that work?
  12. #7
  13. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2013
    Posts
    46
    Rep Power
    2
    This was the solution.

    From Django tutorial:

    You’ll need to restart the development server to see your changes. Normally, the server auto-reloads code every time you modify a file, but the action of creating a new file doesn’t trigger the auto-reloading logic.

    P.S. With older versions your'e supposed to use __unicode__ not __str__

IMN logo majestic logo threadwatch logo seochat tools logo