啊峰哥

第五章 保存用户输入

5.1编写表单,发送POST请求

调整一下lists/templates/home.html中的模版

<html>
    <head>
        <title>To-Do lists</title>
    </head>
    <body>
        <h1>Your To-Do list</h1>
        <form method="POST">
            <input name="id_new_item" id="id_new_item" placeholder="Enter a to-do item"/>
        </form>

        <table id="id_list_table">
        </table>
    </body>
</html>

此时运行,在Django的调试页面,会显示有CSRF错误,所以,使用‘模版标签’’,添加CSRF令牌

<html>
    <head>
        <title>To-Do lists</title>
    </head>
    <body>
        <h1>Your To-Do list</h1>
        <form method="POST">
            <input name="id_new_item" id="id_new_item" placeholder="Enter a to-do item"/>
            {% csrf_token %}
        </form>

        <table id="id_list_table">
        </table>
    </body>
</html>

运行功能测试,出现预期的失败

5.2在服务器中处理POST请求

修改视图函数,让它能够处理POST请求

#tests.py
# -*- coding: utf-8 -*-
from django.test import TestCase
from django.http import HttpRequest
from django.core.urlresolvers import resolve
from lists.views import home_page
from django.template.loader import render_to_string

class HomePageTest(TestCase):
    def test_root_url_resolves_to_home_page_view(self):
        found = resolve('/')
        self.assertEqual(found.func,home_page)

    def test_home_page_return_correct_html(self):
        request = HttpRequest()
        response = home_page(request)
        except_html = render_to_string('home_html')
        self.assertEqual(response.content.decode(),except_html)
        # .deocde()把字节转换为unicode字符串
    
    def test_home_page_can_return_a_POST_request(self):
        request = HttpRequest()
        request.method = 'POST'
        request.POST['item_text'] = 'A new list item'
        
        response = home_page(request)
        self.assertIn('A new list item',response.content.decode())

python3 manage.py test 出现预期失败

为了让测试通过,添加一个if语句,为POST请求提供不同的代码执行路径

#views
from django.shortcuts import render,redirect
from django.http import HttpResponse

def home_page(request):
    if request.method == 'POST':
        return HttpResponse(request.POST['item_text'])
    return render(request,'home.html')

接下来,把POST请求的数据添加到首页模版的表格里

5.3Python变量传入模版渲染

在模版中引入python对象,使用{{}}

<html>
    <head>
        <title>To-Do lists</title>
    </head>
    <body>
        <h1>Your To-Do list</h1>
        <form method="POST">
            <input name="id_new_item" id="id_new_item" placeholder="Enter a to-do item"/>
            {% csrf_token %}
        </form>

        <table id="id_list_table">
            <tr><td>{{ new_item_text }}</td></tr>
        </table>
    </body>
</html>

测试new_item_text的值是否正确,在tests.py添加

self.assertIn('A new list item',response.content.decode()) 
expect_html = render_to_string('home.html',{'new_item_text':'A new list item'})
self.assertEqual(response.content.decode(),expect_html)

重写视图函数,把POST请求中的参数传入模版

#views
from django.shortcuts import render,redirect
from django.http import HttpResponse

def home_page(request):
    return render(request,'home.html',{'new_item_text':request.POST.get('item_text',''),})

修改functioncal.py中的any用法,如果把1:去掉的话,测试应该能通过

#self.assertTrue(any(row.text == '1: Buy peacock feathers' for row in rows))
#替代为 
self.assertIn('1: Buy peacock feathers',[row.text for row in rows])

接下来,扩充功能测试,在functioncal.py中检查表格中添加的第二个待办事项。

事不过三,三则重构,只有test_开头的方法才会作为测试运行,我们添加辅助函数的方法,置于tearDown 和第一个测试之间,用于添加待办事项

# functional_tests.py
# -*- coding: utf-8 -*-
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
import unittest

class NewVisitorTest(unittest.TestCase):
    #setup 和tearDowm是特殊的方法,分别在测试的前后运行,这两个方法与try/except相似
    def setUp(self):
        self.browser = webdriver.Chrome()
        self.browser.implicitly_wait(3)                            #隐式等待 3秒

    def tearDown(self):
        self.browser.quit()

    def check_for_row_in_item(self,row_text):
        table = self.browser.find_element_by_id('id_list_table')
        rows = table.find_elements_by_tag_name('tr')
        self.assertIn(row_text,[row.text for row in rows])
        
    def test_can_start_a_list_and_retrieve_it_later(self):         #名字以test开头的函数都是测试方法
        self.browser.get('http://localhost:8000')
        #网页头部和标题是否含有To-Do这个词
        self.assertIn('To-Do',self.browser.title)
        header_text = self.browser.find_element_by_id('id_new_item').text
        #self.assertIn('To-Do', header_text)    #不知道为什么,错误
        #待办事项
        inputbox = self.browser.find_element_by_id('id_new_item')
        self.assertEqual(inputbox.get_attribute('placeholder'),'Enter a to-do item')   #获取属性placeholder的值
        #发送第一个
        inputbox.send_keys('Buy peacock feathers')
        inputbox.send_keys(Keys.ENTER)

        self.check_for_row_in_item('1: Buy peacock feathers')
        #第二个
        inputbox = self.browser.find_element_by_id('id_new_item')
        inputbox.send_keys('Use peacock feathers to make a fly')
        inputbox.send_keys(Keys.ENTER)

        self.check_for_row_in_item('1: Buy peacock feathers')
        self.check_for_row_in_item('2: Use peacock feathers to make a fly')

        self.fail('Finish the test!')

if __name__ == '__main__':
    unittest.main(warnings='ignore')                                #warnings='ignore'为禁止抛出resourceWarning异常

5.3Django ORM和第一个模型

“对象关系映射器”(ORM) 是一个数据抽象层,描述储存在数据库中的表,列,行。接下来在单元测试按照指定的方式中使用ORM

在tests.py中新建一个类

from lists.models import Item

class HomePageTest(TestCase):。。。

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.assertEqual(saved_items.count(),2)

        first_save_item = saved_items[0]
        second_save_item = saved_items[1]

        self.assertEqual(first_save_item.text,'The first list item')
        self.assertEqual(second_save_item.text, 'The second list item')

在models.py中写入一些代码,添加Item 这个类

 

#models.py

from django.db import models

class Item(models.Model):
    text = models.TextField(default='')   #设置字段类型,并为字段设置默认值,不然会失败

此时运行,会看到一个数据库错误。于是要进行数据库迁徙

python3 manage.py makemigrations 进行数据库迁徙,因为有2个新字段,要迁徙2,

python3 manage.py makemigrations

python3 manage.py test lists运行测试,成功

5.4把POST请求中的数据存入数据库

希望视图把待办事项存入数据库,而不是直接传给响应,于是在测试方法中的test_home_page_can_return_a_POST_request中,添加新代码

    def test_home_page_can_return_a_POST_request(self):
        request = HttpRequest()
        request.method = 'POST'
        request.POST['item_text'] = 'A new list item'

        response = home_page(request)
        
        self.assertEqual(Item.count(),1)
        new_item = Item.objects.first()
        self.assertEqual(new_item.text,'A new list item')
        
        self.assertIn('A new list item',response.content.decode())
        expect_html = render_to_string('home.html',{'new_item_text':'A new list item'})
        self.assertEqual(response.content.decode(),expect_html)

运行报错,0!= 1,

修改一下视图

#views
from django.shortcuts import render,redirect
from django.http import HttpResponse
from lists.models import Item

def home_page(request):
    item = Item()
    item.text = request.POST.get('item_text','')
    item.save()
    
    return render(request,'home.html',item.text})

运行通过

然后,在定义一个新测试方法

class HomePageTest(TestCase):
。。。
def test_home_page_only_save_items_when_necessary(self):
        request = HttpRequest()
        home_page(request)
        self.assertEqual(Item.objects.count(),0)

再更改视图文件

#views
from django.shortcuts import render,redirect
from django.http import HttpResponse
from lists.models import Item

def home_page(request):
    if request.method == 'POST':
        new_item_text = request.POST['item_text']
        Item.objects.create(text=new_item_text) #使用.obejcts.create是创建新Item对象的简化方式,无需再用.save()方法
    else:
        new_item_text = ''

    return render(request,'home.html',{'new_item_text':new_item_text})

运行。通过

5.5处理完POST请求后重定向到首页

修改views.py

#views
from django.shortcuts import render,redirect
from django.http import HttpResponse
from lists.models import Item

def home_page(request):
    if request.method == 'POST':
        Item.objects.creat(text=request.POST['item_text'])
        redirect('/')
        
    return render(request,'home.html')

5.6在模版中渲染待办事项

编写一个单元测试,检测模版是否显示多个待办事项

#test.py 

class HomePageTest(TestCase):
。。。
    def test_home_page_displays_all_list_items(self):
        Item.objects.create(text='itemy 1')
        Item.objects.create(text='itemy 2')

        request = HttpRequest()
        response = home_page(request)
        self.assertIn('itemy 1', response.content.decode())
        self.assertIn('itemy 2', response.content.decode())

运行。失败

在home.html中编写遍历标签

<table id="id_list_table">
        {% for item in items %}
            <tr><td>{{ forloop.counter }}: {{ item.text }}</td></tr>
        {% endfor %}
</table>

同时,把视图待办事项传入模版

#views
from django.shortcuts import render,redirect
from django.http import HttpResponse
from lists.models import Item

def home_page(request):
    if request.method == 'POST':
        Item.objects.create(text=request.POST['item_text'])
        #print(redirect('/'))
        return redirect('/')
    items = Item.objects.all()
    return render(request,'home.html',{'items':items})

运行,出现'no such table lists_item'错误

5.7使用迁徙创建生产数据库

pyhon3 manage.py migrate创建数据库

同时使用forloop.counter解决序号问题

{% for item in items %}
        <tr><td>{{ forloop.counter }}: {{ item.text }}</td></tr>
{% endfor %}

运行。成功。。。

 

 

 

posted on 2017-10-04 14:08  啊峰哥  阅读(277)  评论(0编辑  收藏  举报

导航