• 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
    

     记住实现模型变更的三个步骤:

    有单独的命令来制作和应用迁移的原因是因为您将提交迁移到您的版本控制系统并将其发送到您的应用程序;它们不仅可以使您的开发更容易,而且还可以被其他开发人员和生产中使用。

     

    测试:

    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
    
  • 相关阅读:
    oracle lengthb
    layui-rp
    1709052基于框架新建 子项目
    echar 常用单词
    Leetcode 481.神奇字符串
    Leetcode 480.滑动窗口中位数
    Leetcode 479.最大回文数乘积
    Leetcode 477.汉明距离总和
    Leetcode 476.数字的补数
    Leetcode 475.供暖气
  • 原文地址:https://www.cnblogs.com/lanqie/p/7724872.html
Copyright © 2020-2023  润新知