laravel 简易版搭建
Posted MvloveYouForever
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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
总结数据库操作流程
- 编辑
models.py
文件,改变模型。 - 运行
python manage.py makemigrations
为模型的改变生成迁移文件。 - 运行
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‘...
以上是关于laravel 简易版搭建的主要内容,如果未能解决你的问题,请参考以下文章