3

I'm learning Django from djangobook and in Chapter 6, where the admin app is installed, the syncdb command returns a syntax error in models.py (return u'%s %s' % (self.first_name, self.last_name)

I'm using Python 3.

Why this happens? How to fix it?

models.py:

from django.db import models

class Publisher (models.Model):
    name = models.CharField(max_length=30)
    address = models.CharField(max_length=50)
    city = models.CharField(max_length=60)
    state_province = models.CharField(max_length=30)
    country = models.CharField(max_length=50)
    website=models.URLField()

    def __unicode__(self):
        return self.name

class Author(models.Model):
    first_name = models.CharField(max_length=30)
    last_name = models.CharField(max_length=40)
    email = models.EmailField()

    def __unicode__(self):
        return u'%s %s' % (self.first_name, self.last_name)

class Book(models.Model):
    title = models.CharField(max_length=100)
    authors = models.ManyToManyField(Author)
    publisher = models.ForeignKey(Publisher)
    publication_date = models.DateField()

    def __unicode__(self):
        return self.title

Error:

  File "C:\Users\ken\Desktop\NightHawk\Hawk\books\models.py", line 20
    return u'%s %s' % (self.first_name, self.last_name)
                  ^
SyntaxError: invalid syntax
7
  • Please show us the full exception, plus more context; the lines before should be included. Commented May 17, 2014 at 14:29
  • I think that with Python 3, you need to define __str__ methods instead of __unicode__. Check if that works for you. Commented May 17, 2014 at 14:40
  • @Salem is correct. You need to use __str__ but I'd also like to point out that the djangobook (by its own admission) is horribly out of date. I'd recommend that you try effective django or two scoops of django. Best of luck! Commented May 17, 2014 at 14:43
  • @KarthikeyanKC weird, that seems right to me. Either way, try to replace it with return "{0} {1}".format(self.first_name, self.last_name) Commented May 17, 2014 at 14:50
  • @Salem: that wouldn't throw a syntax error, though. Commented May 17, 2014 at 14:51

1 Answer 1

6

You are using Python 2 syntax in Python 3. Although Python 3.3 and up support using the u prefix for unicode string values (to support cross-version code), Python 3.2 and before do not; remove the u prefix to make it work:

def __unicode__(self):
    return '%s %s' % (self.first_name, self.last_name)

However, you will also need to consult the Django Porting to Python 3 documentation and use __str__, not __unicode__ here:

def __str__(self):
    return '%s %s' % (self.first_name, self.last_name)

If you are following a specific book, you may want to install Python 2 instead.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.