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
__str__methods instead of__unicode__. Check if that works for you.__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!return "{0} {1}".format(self.first_name, self.last_name)