python - 如何在测试期间将参数传递给请求?
问题描述
我通过 Client() 运行测试。post,但 request.POST 不包含传递的字典
测试.py
from django.test import TestCase
from django.test.client import Client
class AccountTests(TestCase):
def setUp(self):
self.email = 's@s/com'
self.name = 'John'
self.mobile = "+799999999"
def test_user_login(self):
c = Client()
response = c.post('/login-otp/',
{'email': self.email, 'name': self.name, 'mobile': self.mobile},
follow=True)
视图.py
def login_otp(request):
mobile = request.session['mobile'] # the interpreter does not see the "mobile" key
context = {'mobile': mobile}
if request.method == 'POST':
otp = request.POST.get('otp')
解决方案
您没有mobile
作为 POST 参数传递。
试试这个:
def login_otp(request):
mobile = request.POST['mobile']
...
编辑:
您可以像这样设置会话变量:https ://docs.djangoproject.com/en/3.0/topics/testing/tools/#django.test.Client.session
推荐阅读
- ipc - 消息队列与 TCP IP 套接字。哪个IPC在linux中更快?
- html - 嵌套在 CSS 网格中
- php - php 图片返回空白
- r - 如果满足行条件,如何从另一个数据框中获取值?
- r - RODBC(SQL Server)为转换为数字的长字符字段提供不一致的结果
- php - 有人可以解释一下这种行为吗(PHP,foreach)
- php - 如何在 VestaCP (CentOS) 中使用 Firebird InterBase 运行 PHP
- java - 使用 Java 8 流将十六进制字符串转换为 ByteBuffer
- php - 安装 magento 时出现 Composer 错误
- php - PHP-解析错误:语法错误,意外的'readphp'(T_STRING),期望','或';'