➜ python manage.py shell
Python 3.6.8 (default, Aug 20 2019, 17:12:48)
Type 'copyright', 'credits' or 'license' for more information
IPython 7.8.0 An enhanced Interactive Python. Type '?' for help.
In [1]: from django.test import Client
In [2]: c = Client()
In [3]: response = c.get('/blog/')
In [4]: response.status_code
Out[4]: 200
In [5]: response.content
Out[5]: b'\n<!DOCTYPE html>\n<html>\n<head>\n\t<title>My Blog</title>\n\t<link href="/static/css/blog.css" rel="stylesheet">\n</head>\n<body>\n\t<div id="content">\n\t\t\n\t<h1>My Blog</h1>\n\t\n\t\t<h2>\n\t\t\t<a href="/blog/2019/10/16/my-second-post/">\n\t\t\t\tmy second post\n\t\t\t</a>\n\t\t</h2>\n\t\t<p class="date">\n\t\t\tPublished Oct. 16, 2019, 7:15 a.m. by admin\n\t\t</p>\n\t\t<p>Nice</p>\n\t\n\n\t</div>\n\t<div id="sidebar">\n\t\t<h2>My blog</h2>\n\t\t<p>This is my blog</p>\n\t</div>\n</body>\n</html>'
您也可以编写自己的management commands,并用类似的方法调用它们
(您编写的部分使用Python Argparse解析您的命令)
您可以使用Django测试客户机。你知道吗
在django shell中尝试以下代码
看看官方文档Test Client
相关问题 更多 >
编程相关推荐