laravel 简易版搭建

// 安装mysql驱动,并配置过mysql地址之后

// 生成数据库表
python manage.py migrate

// 创建 polls应用 
python manage.py startapp polls
// settings.py 中 添加应用
INSTALLED_APPS = [
    'polls.apps.PollsConfig',
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
]

// 创建模型
from django.db import models
//  polls/models.py 中

class Question(models.Model):
    question_text = models.CharField(max_length=200)
    pub_date = models.DateTimeField('date published')


class Choice(models.Model):
    question = models.ForeignKey(Question, on_delete=models.CASCADE)
    choice_text = models.CharField(max_length=200)
    votes = models.IntegerField(default=0)

//  创建数据迁移
 python manage.py makemigrations polls

//   数据迁移对应的 sql
python manage.py sqlmigrate polls 0001

// 再次数据迁移
python manage.py migrate

 总结数据库操作流程

创建管理员账号

python manage.py createsuperuser

// 在 polls/admin.py 中,将Question加入管理

from django.contrib import admin

from .models import Question

admin.site.register(Question)

 创建测试

//  将下面的代码写入 polls 应用里的 tests.py 文件内:

import datetime

from django.test import TestCase
from django.utils import timezone

from .models import Question


class QuestionModelTests(TestCase):

# 测试方法必须test 开头
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)

运行测试

python manage.py test polls

## 结果
Creating test database for alias 'default'...
System check identified no issues (0 silenced).
F
======================================================================
FAIL: test_was_published_recently_with_future_question (polls.tests.QuestionModelTests)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/path/to/mysite/polls/tests.py", line 16, in test_was_published_recently_with_future_question
    self.assertIs(future_question.was_published_recently(), False)
AssertionError: True is not False

----------------------------------------------------------------------
Ran 1 test in 0.001s

FAILED (failures=1)
Destroying test database for alias 'default'...

猜你喜欢

转载自www.cnblogs.com/Mvloveyouforever/p/10492684.html