时间:2022-06-30 09:28:44 | 栏目:Python代码 | 点击:次
在django
中,ORM(对象关系映射器—object-relational mapper
)任务是:模型化数据库,创建数据库由另外一个系统负责(迁移–migration
),迁移任务是根据对models.py
文件的改动情况,添加或者删除表和列
依然报错:
models.py
from django.db import models class Item(models.Model): text=models.TextField(default='')
tests.py
'''from django.test import TestCase # Create your tests here. class Smokeclass(TestCase): def test_bad_maths(self): self.assertEquals(1+1,3)''''' from django.urls import resolve from django.test import TestCase from lists.views import home_page from django.http import HttpRequest from lists.models import Item class HomePageTest(TestCase): def test_root_url_resolve_to_home_page_view(self): found=resolve('/') # resolve函数是django内部使用的函数,用于解析url, # 并且将其映射到相应的视图函数上,检查网站根路径时"/", # 是否能找到home_page函数 self.assertEquals(found.func,home_page) def test_home_page_returns_correct_html(self): request=HttpRequest() # 创建httprequest对象,用户在浏览器中请求网页时 # django看到的就是httprequest对象 response=home_page(request) # 把对象传给home_page视图 html=response.content.decode('utf8') # 提取content,得到结果是原始的字节,即发个用户 # 浏览器的0和1,随后调用.decode(),把原始字节 # 转换成发给用户的html字符串 self.assertTrue(html.startswith('<html>')) self.assertIn('<title>To-Do lists</title>',html) self.assertTrue(html.endswith('</html>)) self.assertTemplateUsed(response,'home.html') def test_user_home_template(self): response=self.client.get('/') self.assertTemplateUsed(response,'home.html') def test_can_save_a_POST_request(self): response=self.client.post('/',data={'item_text':'a new list item'}) self.assertIn('a new list item',response.content.decode()) self.assertTemplateUsed(response, 'home.html') class ItemModelTest(TestCase): def test_saving_and_retrieving_items(self): first_item=Item() first_item.text="the first list item" first_item.save() second_item = Item() second_item.text = "the second list item" second_item.save() saved_items=Item.objects.all() self.assertEquals(saved_items.count(),2) first_saved_item=saved_items[0] second_saved_item=saved_items[1] self.assertEquals(first_saved_item.text,'the first list item') self.assertEquals(second_saved_item.text, 'the second list item')
python manage.py makemigrations