django 笔记
创建一个项目
django-admin startproject mysite
验证一下你的Django项目是否工作
python manage.py runserver
python manage.py runserver 8080
python manage.py runserver 0:8000
创建投票应用程序
python manage.py startapp polls
数据库中创建表
python manage.py migrate
通过运行makemigrations告诉Django,已经对模型做了一些更改(在这个例子中,你创建了一个新的模型)并且会将这些更改记录为迁移文件。
python manage.py makemigrations polls
sqlmigrate命令接收迁移文件的名字并返回它们的SQL语句
python manage.py sqlmigrate polls 0001
记住实现模型变更的三个步骤:
- 修改你的模型(在
models.py文件中)。 - 运行
python manage.py makemigrations,为这些修改创建迁移文件 - 运行
python manage.py migrate,将这些改变更新到数据库中。
有单独的命令来制作和应用迁移的原因是因为您将提交迁移到您的版本控制系统并将其发送到您的应用程序;它们不仅可以使您的开发更容易,而且还可以被其他开发人员和生产中使用。
测试:
import datetime
from django.utils import timezone
from django.test import TestCase
from .models import Question
class QuestionModelTests(TestCase):
def test_was_published_recently_with_future_question(self):
"""
was_published_recently() returns False for questions whose pub_date
is in the future.
"""
time = timezone.now() + datetime.timedelta(days=30)
future_question = Question(pub_date=time)
self.assertIs(future_question.was_published_recently(), False)
def test_was_published_recently_with_old_question(self):
"""
was_published_recently() returns False for questions whose pub_date
is older than 1 day.
"""
time = timezone.now() - datetime.timedelta(days=1, seconds=1)
old_question = Question(pub_date=time)
self.assertIs(old_question.was_published_recently(), False)
def test_was_published_recently_with_recent_question(self):
"""
was_published_recently() returns True for questions whose pub_date
is within the last day.
"""
time = timezone.now() - datetime.timedelta(hours=23, minutes=59, seconds=59)
recent_question = Question(pub_date=time)
self.assertIs(recent_question.was_published_recently(), True)
python manage.py test polls

浙公网安备 33010602011771号