安装验证:
tar -zxvf tar -zxvf Django-1.9.tar.gz
cd Django-1.9/
python setup.py install
python
>>> import django
>>> print(django.get_version)
>>> print(django.get_version())
1.9
新建项目:
django-admin.py startproject king
vi /opt/king/king/settings.py
# Application definition
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
#wc add1
'king.jobs',
]
# Database
# https://docs.djangoproject.com/en/1.9/ref/settings/#databases
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
#wc add2
'USER': root,
'PASSWORD': 123456,
'HOST': localhost,
'PORT': 3306,
}
}
vi /opt/king/jobs/models.py
# Create your models here.
#wc add1
class Location(models.Model):
city = models.CharField(maxlength=50)
state = models.CharField(maxlength=50, null=True, blank=True)
country = models.CharField(maxlength=50)
def __str__(self):
if self.state:
return "%s, %s, %s" % (self.city, self.state, self.country)
else:
return "%s, %s" % (self.city, self.country)
class Job(models.Model):
pub_date = models.DateField()
job_title = models.CharField(maxlength=50)
job_description = models.TextField()
location = models.ForeignKey(Location)
def __str__(self):
return "%s (%s)" % (self.job_title, self.location)
cd ..
python manage.py sql jobs