51Testing软件测试论坛

标题: 跪求解决 python django.test Client () 报错解决方案 [打印本页]

作者: 测试积点老人    时间: 2019-8-19 09:49
标题: 跪求解决 python django.test Client () 报错解决方案

问题背景:
目前在使用python 中的 django.test Client() 类进行客户端测试,遇到报错信息‘Invalid HTTP_HOST header: 'testserver'. You may need to add 'testserver' to ALLOWED_HOSTS.’ 报错,无法解决;

1、Django -v:1.10.3
2、python -v:python3
3、web项目已经搭建成功:首页地址 [color=rgb(0, 105, 214) !important]http://127.0.0.1:8000/index/
可正常访问;

使用 Django内置 Client()类模拟get() 和 post()请求方式进行客户端验证;

cmd.exe

  1. from django.test import Client
  2. c=Client()
  3. response=c.get('/index')
  4. 运行到这步的时候,就报‘‘Invalid HTTP_HOST header: 'testserver'. You may need to add 'testserver' to ALLOWED_HOSTS’ 错误了
复制代码

解决方法:

1、setting.py 中设置

DEBUG=True
ALLOWED_HOSTS = ['*'] / ALLOWED_HOSTS = ['127.0.0.1','localhost','testserver']

(因为看到网上说 当DEBUG为True并且ALLOWED_HOSTS为空时,主机将针对['localhost','127.0.0.1','[:: 1]']进行验证。)

DEBUG=False
ALLOWED_HOSTS = ['*'] / ALLOWED_HOSTS = ['127.0.0.1','localhost','testserver']

以上均无效。跪求大神帮忙解答,在线等……







欢迎光临 51Testing软件测试论坛 (http://bbs.51testing.com/) Powered by Discuz! X3.2