跑Django不带s

2024-04-26 21:18:03 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个Django的小网站mysite。我希望能够运行我的Django应用程序而不运行web服务器。我不想使用HTTP客户端发出请求,而是编写如下内容:

django.run()
result = django_request('/foo/bar')

所以Django仍然会做URL解析等,只是不通过UWSGI提供服务。这合理吗?你知道吗

我的最终目标是对web应用程序的端点进行端到端的基准测试,除了I/O


Tags: djangorun服务器web应用程序http客户端内容
2条回答

您也可以编写自己的management commands,并用类似的方法调用它们

./manage.py my_foo_command  bar=baz  output=baz.txt

(您编写的部分使用Python Argparse解析您的命令)

您可以使用Django测试客户机。你知道吗

在django shell中尝试以下代码

➜ 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>'

看看官方文档Test Client

相关问题 更多 >