django-admin.py startproject mysite

python manage.py runserver 0.0.0.0:8000
 
vi mysite/settings.py
TIME_ZONE = 'Asia/Chongqing'
 
DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.mysql',
        'NAME': 'mysite',                    
        'USER': 'xxx',
        'PASSWORD': 'xx',
 
python manage.py startapp polls
 
mysql> create database mysite default charset utf8 COLLATE 
 
utf8_general_ci;
 
grant all on mysite.* to name@'localhost'  identified by 'psswd';
 
vi polls/models.py
class Poll(models.Model):
    question = models.CharField(max_length=200)
    pub_date = models.DateTimeField('date published')
 
class Choice(models.Model):
    poll = models.ForeignKey(Poll)
    choice_text = models.CharField(max_length=200)
    votes = models.IntegerField(default=0)
 
python manage.py sql polls
 
python manage.py syncdb
 
python manage.py shell
 
>>> from polls.models import Poll, Choice
>>> Poll.objects.all()
[]
>>> from django.utils import timezone
>>> p = Poll(question="What's new?", pub_date=timezone.now())
>>> p.save()
>>> p.id
 
polls/models.py
自定议方法
import datetime
from django.utils import timezone
# ...
class Poll(models.Model):
# ...
def was_published_recently(self):
return self.pub_date >= timezone.now() - datetime.timedelta(days=1)