我正在尝试测试在会话中存储经过验证的表单数据的视图。但是,当我尝试测试时,我得到了会话值和错误的无:
AssertionError:'example_postcode'!=无
测试是:
class TestSignup(TestCase):
def test_postcode(self):
postcode = 'example_postcode'
self.client.post('/users/postcode/', {'postcode': postcode})
self.assertEqual(postcode, self.client.session.get('postcode'))
视图是:
@require_http_methods(['GET', 'HEAD', 'POST'])
def postcode_view(request,):
if request.method == 'POST':
form = PostcodeForm(request.POST)
if form.is_valid():
request.session.update(form.cleaned_data)
return redirect('users:signup_2')
else:
form = form_class()
context.update({'form': form})
return render(request, 'pages/signup_postcode.html', context)
形式
class PostcodeForm(forms.Form):
postcode = GBPostcodeField(label='Please enter your postcode',
error_messages={'invalid': 'Please enter a valid postcode'})
答案 0 :(得分:0)
其他人的上述评论证实,测试基本上是正确的,错误是一个尾随斜线,现在更新。